- DCI transformations implemented
[public/netxms.git] / src / nxscript / nxscript.cpp
CommitLineData
cce155af
VK
1/*
2** NetXMS - Network Management System
3** NetXMS Scripting Host
4** Copyright (C) 2005 Victor Kirhenshtein
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 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 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** $module: nxscript.cpp
21**
22**/
23
24#include "nxscript.h"
25
26
9e52272a
VK
27static NXSL_TestClass *m_pTestClass;
28
29
cce155af
VK
30static char *LoadFile(char *pszFileName, DWORD *pdwFileSize)
31{
32 int fd, iBufPos, iNumBytes, iBytesRead;
33 char *pBuffer = NULL;
34 struct stat fs;
35
36 fd = open(pszFileName, O_RDONLY | O_BINARY);
37 if (fd != -1)
38 {
39 if (fstat(fd, &fs) != -1)
40 {
41 pBuffer = (char *)malloc(fs.st_size + 1);
42 if (pBuffer != NULL)
43 {
44 *pdwFileSize = fs.st_size;
45 for(iBufPos = 0; iBufPos < fs.st_size; iBufPos += iBytesRead)
46 {
47 iNumBytes = min(16384, fs.st_size - iBufPos);
48 if ((iBytesRead = read(fd, &pBuffer[iBufPos], iNumBytes)) < 0)
49 {
50 free(pBuffer);
51 pBuffer = NULL;
52 break;
53 }
54 }
55 }
56 }
57 close(fd);
58 }
59 return pBuffer;
60}
61
62
9e52272a
VK
63int F_new(int argc, NXSL_Value **argv, NXSL_Value **ppResult)
64{
65 *ppResult = new NXSL_Value(new NXSL_Object(m_pTestClass, NULL));
66 return 0;
67}
68
69
cce155af
VK
70//
71// main()
72//
73
74int main(int argc, char *argv[])
75{
76 char *pszSource, szError[1024];
77 DWORD dwSize;
b36dc6ff
VK
78 NXSL_Program *pScript;
79 NXSL_Environment *pEnv;
9e52272a
VK
80 NXSL_Value **ppArgs;
81 NXSL_ExtFunction func;
82 int i;
83
84 func.m_iNumArgs = 0;
85 func.m_pfHandler = F_new;
86 strcpy(func.m_szName, "new");
87
88 m_pTestClass = new NXSL_TestClass;
cce155af 89
c7a4332a
VK
90 printf("NetXMS Scripting Host Version " NETXMS_VERSION_STRING "\n"
91 "Copyright (c) 2005 Victor Kirhenshtein\n\n");
92
93 if (argc == 1)
94 {
95 printf("Usage: nxscript script [arg1 [... argN]]\n\n");
96 return 127;
97 }
98
cce155af 99 pszSource = LoadFile(argv[1], &dwSize);
b36dc6ff
VK
100 pScript = (NXSL_Program *)NXSLCompile(pszSource, szError, 1024);
101 if (pScript != NULL)
cce155af 102 {
b36dc6ff
VK
103 pScript->Dump(stdout);
104 pEnv = new NXSL_Environment;
105 pEnv->SetIO(stdin, stdout);
9e52272a
VK
106 pEnv->RegisterFunctionSet(1, &func);
107
108 // Prepare arguments
109 if (argc > 2)
110 {
111 ppArgs = (NXSL_Value **)malloc(sizeof(NXSL_Value *) * (argc - 2));
112 for(i = 2; i < argc; i++)
113 ppArgs[i - 2] = new NXSL_Value(argv[i]);
114 }
115 else
116 {
117 ppArgs = NULL;
118 }
119
120 if (pScript->Run(pEnv, argc - 2, ppArgs) == -1)
0b26d595 121 {
b36dc6ff 122 printf("%s\n", pScript->GetErrorText());
0b26d595 123 }
b36dc6ff 124 delete pScript;
9e52272a 125 safe_free(ppArgs);
cce155af
VK
126 }
127 else
128 {
129 printf("%s\n", szError);
130 }
131 return 0;
132}