"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/agent/libnxagent/procexec.cpp" (23 Feb 2021, 4385 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "procexec.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** NetXMS - Network Management System
    3 ** Copyright (C) 2003-2020 Raden Solutions
    4 **
    5 ** This program is free software; you can redistribute it and/or modify
    6 ** it under the terms of the GNU Lesser General Public License as published
    7 ** by the Free Software Foundation; either version 3 of the License, or
    8 ** (at your option) any later version.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 ** GNU General Public License for more details.
   14 **
   15 ** You should have received a copy of the GNU Lesser General Public License
   16 ** along with this program; if not, write to the Free Software
   17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18 **
   19 ** File: procexec.cpp
   20 **
   21 **/
   22 
   23 #include "libnxagent.h"
   24 
   25 /**
   26  * Create new key-value executor object
   27  */
   28 KeyValueOutputProcessExecutor::KeyValueOutputProcessExecutor(const TCHAR *command, bool shellExec) : ProcessExecutor(command, shellExec)
   29 {
   30    m_sendOutput = true;
   31    m_separator = _T('=');
   32 }
   33 
   34 /**
   35  * Key-value executor output handler
   36  */
   37 void KeyValueOutputProcessExecutor::onOutput(const char *text)
   38 {
   39    TCHAR *buffer;
   40 #ifdef UNICODE
   41    buffer = WideStringFromMBStringSysLocale(text);
   42 #else
   43    buffer = _tcsdup(text);
   44 #endif
   45    TCHAR *newLinePtr = nullptr, *lineStartPtr = buffer, *eqPtr = nullptr;
   46    do
   47    {
   48       newLinePtr = _tcschr(lineStartPtr, _T('\r'));
   49       if (newLinePtr == nullptr)
   50          newLinePtr = _tcschr(lineStartPtr, _T('\n'));
   51       if (newLinePtr != nullptr)
   52       {
   53          *newLinePtr = 0;
   54          m_buffer.append(lineStartPtr);
   55          if (m_buffer.length() > MAX_RESULT_LENGTH * 3)
   56          {
   57             nxlog_debug(4, _T("ParamExec::onOutput(): result too long - %s"), m_buffer.cstr());
   58             stop();
   59             m_buffer.clear();
   60             break;
   61          }
   62       }
   63       else
   64       {
   65          m_buffer.append(lineStartPtr);
   66          if (m_buffer.length() > MAX_RESULT_LENGTH * 3)
   67          {
   68             nxlog_debug(4, _T("ParamExec::onOutput(): result too long - %s"), m_buffer.cstr());
   69             stop();
   70             m_buffer.clear();
   71          }
   72          break;
   73       }
   74 
   75       if (m_buffer.length() > 1)
   76       {
   77          eqPtr = _tcschr(m_buffer.getBuffer(), m_separator);
   78          if (eqPtr != NULL)
   79          {
   80             *eqPtr = 0;
   81             eqPtr++;
   82             Trim(m_buffer.getBuffer());
   83             Trim(eqPtr);
   84             m_data.set(m_buffer.getBuffer(), eqPtr);
   85          }
   86       }
   87       m_buffer.clear();
   88       lineStartPtr = newLinePtr + 1;
   89    } while (*lineStartPtr != 0);
   90 
   91    MemFree(buffer);
   92 }
   93 
   94 /**
   95  * End of output callback
   96  */
   97 void KeyValueOutputProcessExecutor::endOfOutput()
   98 {
   99    if (m_buffer.length() > 0)
  100    {
  101       TCHAR *ptr = _tcschr(m_buffer.getBuffer(), m_separator);
  102       if (ptr != NULL)
  103       {
  104          *ptr = 0;
  105          ptr++;
  106          Trim(m_buffer.getBuffer());
  107          Trim(ptr);
  108          m_data.set(m_buffer.getBuffer(), ptr);
  109       }
  110       m_buffer.clear();
  111    }
  112 }
  113 
  114 /**
  115  * Create new line output executor object
  116  */
  117 LineOutputProcessExecutor::LineOutputProcessExecutor(const TCHAR *command, bool shellExec) : ProcessExecutor(command, shellExec)
  118 {
  119    m_sendOutput = true;
  120 }
  121 
  122 /**
  123  * Line output executor output handler
  124  */
  125 void LineOutputProcessExecutor::onOutput(const char *text)
  126 {
  127    TCHAR *buffer;
  128 #ifdef UNICODE
  129    buffer = WideStringFromMBStringSysLocale(text);
  130 #else
  131    buffer = _tcsdup(text);
  132 #endif
  133    TCHAR *newLinePtr = nullptr, *lineStartPtr = buffer;
  134    do
  135    {
  136       newLinePtr = _tcschr(lineStartPtr, _T('\r'));
  137       if (newLinePtr == nullptr)
  138       {
  139          newLinePtr = _tcschr(lineStartPtr, _T('\n'));
  140       }
  141       else
  142       {
  143          if (*(newLinePtr + 1) == '\n')
  144          {
  145             *newLinePtr = 0;
  146             newLinePtr++;
  147          }
  148       }
  149       if (newLinePtr != nullptr)
  150       {
  151          *newLinePtr = 0;
  152          m_buffer.append(lineStartPtr);
  153       }
  154       else
  155       {
  156          m_buffer.append(lineStartPtr);
  157          break;
  158       }
  159 
  160       m_data.add(m_buffer);
  161       m_buffer.clear();
  162       lineStartPtr = newLinePtr + 1;
  163    } while (*lineStartPtr != 0);
  164 
  165    MemFree(buffer);
  166 }
  167 
  168 /**
  169  * End of output callback
  170  */
  171 void LineOutputProcessExecutor::endOfOutput()
  172 {
  173    if (m_buffer.length() > 0)
  174    {
  175       m_data.add(m_buffer);
  176       m_buffer.clear();
  177    }
  178 }