added quote character escape in function AgentGetParameterArg
[public/netxms.git] / src / agent / libnxagent / tools.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2015 Victor Kirhenshtein
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: tools.cpp
20 **
21 **/
22
23 #include "libnxagent.h"
24
25 /**
26 * Get arguments for parameters like name(arg1,...)
27 * Returns FALSE on processing error
28 */
29 static bool AgentGetParameterArgInternal(const TCHAR *param, int index, TCHAR *arg, int maxSize, bool inBrackets)
30 {
31 const TCHAR *ptr1, *ptr2;
32 int state, currIndex, pos;
33 bool success = true;
34
35 arg[0] = 0; // Default is empty string
36 ptr1 = _tcschr(param, _T('('));
37 if(!inBrackets) //If no brackets is used set beginning of the sting as a parameter beginning
38 ptr1 = param;
39 if (ptr1 == NULL)
40 return true; // No arguments at all
41 for(ptr2 = ptr1 + 1, currIndex = 1, state = 0, pos = 0; state != -1; ptr2++)
42 {
43 switch(state)
44 {
45 case 0: // Normal
46 switch(*ptr2)
47 {
48 case _T(')'):
49 if (currIndex == index)
50 arg[pos] = 0;
51 state = -1; // Finish processing
52 break;
53 case _T('"'):
54 state = 1; // String
55 break;
56 case _T('\''): // String, type 2
57 state = 2;
58 break;
59 case _T(','):
60 if (currIndex == index)
61 {
62 arg[pos] = 0;
63 state = -1;
64 }
65 else
66 {
67 currIndex++;
68 }
69 break;
70 case 0:
71 state = -1; // Finish processing
72 if (!inBrackets) //No error flag if parameters were given without braces
73 success = false; // Set error flag
74 break;
75 default:
76 if ((currIndex == index) && (pos < maxSize - 1))
77 arg[pos++] = *ptr2;
78 }
79 break;
80 case 1: // String in ""
81 switch(*ptr2)
82 {
83 case _T('"'):
84 if (*(ptr2 + 1) != _T('"'))
85 {
86 state = 0; // Normal
87 }
88 else
89 {
90 ptr2++;
91 if ((currIndex == index) && (pos < maxSize - 1))
92 arg[pos++] = *ptr2;
93 }
94 break;
95 case 0:
96 state = -1; // Finish processing
97 success = false; // Set error flag
98 break;
99 default:
100 if ((currIndex == index) && (pos < maxSize - 1))
101 arg[pos++] = *ptr2;
102 }
103 break;
104 case 2: // String in ''
105 switch(*ptr2)
106 {
107 case _T('\''):
108 if (*(ptr2 + 1) != _T('\''))
109 {
110 state = 0; // Normal
111 }
112 else
113 {
114 ptr2++;
115 if ((currIndex == index) && (pos < maxSize - 1))
116 arg[pos++] = *ptr2;
117 }
118 break;
119 case 0:
120 state = -1; // Finish processing
121 success = false; // Set error flag
122 break;
123 default:
124 if ((currIndex == index) && (pos < maxSize - 1))
125 arg[pos++] = *ptr2;
126 }
127 break;
128 }
129 }
130
131 if (success)
132 StrStrip(arg);
133 return success;
134 }
135
136 /**
137 * Get arguments for parameters like name(arg1,...) as multibyte string
138 * Returns FALSE on processing error
139 */
140 bool LIBNXAGENT_EXPORTABLE AgentGetParameterArgA(const TCHAR *param, int index, char *arg, int maxSize, bool inBrackets)
141 {
142 #ifdef UNICODE
143 WCHAR *temp = (WCHAR *)malloc(maxSize * sizeof(WCHAR));
144 bool success = AgentGetParameterArgInternal(param, index, temp, maxSize, inBrackets);
145 if (success)
146 {
147 WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK | WC_DEFAULTCHAR, temp, -1, arg, maxSize, NULL, NULL);
148 arg[maxSize - 1] = 0;
149 }
150 free(temp);
151 return success;
152 #else
153 return AgentGetParameterArgInternal(param, index, arg, maxSize, inBrackets);
154 #endif
155 }
156
157 /**
158 * Get arguments for parameters like name(arg1,...) as UNICODE string
159 * Returns FALSE on processing error
160 */
161 bool LIBNXAGENT_EXPORTABLE AgentGetParameterArgW(const TCHAR *param, int index, WCHAR *arg, int maxSize, bool inBrackets)
162 {
163 #ifdef UNICODE
164 return AgentGetParameterArgInternal(param, index, arg, maxSize, inBrackets);
165 #else
166 char *temp = (char *)malloc(maxSize);
167 bool success = AgentGetParameterArgInternal(param, index, temp, maxSize, inBrackets);
168 if (success)
169 {
170 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, temp, -1, arg, maxSize);
171 arg[maxSize - 1] = 0;
172 }
173 free(temp);
174 return success;
175 #endif
176 }