license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / libnxsl / env.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Scripting Language Interpreter
4 ** Copyright (C) 2003-2010 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published by
8 ** the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: env.cpp
21 **
22 **/
23
24 #include "libnxsl.h"
25
26
27 //
28 // Externals
29 //
30
31 int F_abs(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
32 int F_classof(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
33 int F_d2x(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
34 int F_exit(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
35 int F_exp(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
36 int F_gmtime(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
37 int F_index(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
38 int F_left(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
39 int F_length(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
40 int F_localtime(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
41 int F_log(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
42 int F_log10(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
43 int F_lower(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
44 int F_ltrim(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
45 int F_max(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
46 int F_min(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
47 int F_pow(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
48 int F_right(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
49 int F_rindex(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
50 int F_rtrim(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
51 int F_strftime(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
52 int F_substr(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
53 int F_time(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
54 int F_trace(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
55 int F_trim(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
56 int F_typeof(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
57 int F_upper(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
58 int F_AddrInRange(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
59 int F_AddrInSubnet(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
60 int F_SecondsToUptime(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_Program *program);
61
62
63 //
64 // Default built-in function list
65 //
66
67 static NXSL_ExtFunction m_builtinFunctions[] =
68 {
69 { "_exit", F_exit, -1 },
70 { "abs", F_abs, 1 },
71 { "classof", F_classof, 1 },
72 { "d2x", F_d2x, -1 },
73 { "exp", F_exp, 1 },
74 { "gmtime", F_gmtime, -1 },
75 { "index", F_index, -1 },
76 { "left", F_left, -1 },
77 { "length", F_length, 1 },
78 { "localtime", F_localtime, -1 },
79 { "log", F_log, 1 },
80 { "log10", F_log10, 1 },
81 { "lower", F_lower, 1 },
82 { "ltrim", F_ltrim, 1 },
83 { "max", F_max, -1 },
84 { "min", F_min, -1 },
85 { "pow", F_pow, 2 },
86 { "right", F_right, -1 },
87 { "rindex", F_rindex, -1 },
88 { "rtrim", F_rtrim, 1 },
89 { "strftime", F_strftime, -1 },
90 { "substr", F_substr, -1 },
91 { "time", F_time, 0 },
92 { "trace", F_trace, 2 },
93 { "trim", F_trim, 1 },
94 { "typeof", F_typeof, 1 },
95 { "upper", F_upper, 1 },
96 { "AddrInRange", F_AddrInRange, 3 },
97 { "AddrInSubnet", F_AddrInSubnet, 3 },
98 { "SecondsToUptime", F_SecondsToUptime, 1 }
99 };
100
101
102 //
103 // Constructor
104 //
105
106 NXSL_Environment::NXSL_Environment()
107 {
108 m_dwNumFunctions = sizeof(m_builtinFunctions) / sizeof(NXSL_ExtFunction);
109 m_pFunctionList = (NXSL_ExtFunction *)nx_memdup(m_builtinFunctions, sizeof(m_builtinFunctions));
110 m_pStdIn = NULL;
111 m_pStdOut = NULL;
112 m_pLibrary = NULL;
113 }
114
115
116 //
117 // Destructor
118 //
119
120 NXSL_Environment::~NXSL_Environment()
121 {
122 safe_free(m_pFunctionList);
123 }
124
125
126 //
127 // Find function by name
128 //
129
130 NXSL_ExtFunction *NXSL_Environment::findFunction(char *pszName)
131 {
132 DWORD i;
133
134 for(i = 0; i < m_dwNumFunctions; i++)
135 if (!strcmp(m_pFunctionList[i].m_szName, pszName))
136 return &m_pFunctionList[i];
137 return NULL;
138 }
139
140
141 //
142 // Register function set
143 //
144
145 void NXSL_Environment::registerFunctionSet(DWORD dwNumFunctions, NXSL_ExtFunction *pList)
146 {
147 m_pFunctionList = (NXSL_ExtFunction *)realloc(m_pFunctionList, sizeof(NXSL_ExtFunction) * (m_dwNumFunctions + dwNumFunctions));
148 memcpy(&m_pFunctionList[m_dwNumFunctions], pList, sizeof(NXSL_ExtFunction) * dwNumFunctions);
149 m_dwNumFunctions += dwNumFunctions;
150 }
151
152
153 //
154 // Find module by name
155 //
156
157 BOOL NXSL_Environment::useModule(NXSL_Program *pMain, const TCHAR *pszName)
158 {
159 TCHAR *pData, szBuffer[MAX_PATH];
160 DWORD dwSize;
161 NXSL_Program *pScript;
162 BOOL bRet = FALSE;
163
164 // First, try to find module in library
165 if (m_pLibrary != NULL)
166 {
167 pScript = m_pLibrary->findScript(pszName);
168 if (pScript != NULL)
169 {
170 pMain->useModule(pScript, pszName);
171 bRet = TRUE;
172 }
173 }
174
175 // If failed, try to load it from file
176 if (!bRet)
177 {
178 _sntprintf(szBuffer, MAX_PATH, "%s.nxsl", pszName);
179 pData = NXSLLoadFile(szBuffer, &dwSize);
180 if (pData != NULL)
181 {
182 pScript = (NXSL_Program *)NXSLCompile(pData, NULL, 0);
183 if (pScript != NULL)
184 {
185 pMain->useModule(pScript, pszName);
186 delete pScript;
187 bRet = TRUE;
188 }
189 free(pData);
190 }
191 }
192
193 return bRet;
194 }
195
196
197 //
198 // Write trace message
199 // Default implementation does nothing
200 //
201
202 void NXSL_Environment::trace(int level, const TCHAR *text)
203 {
204 }