license changed to LGPL for libnxcl, libnxsnmp, libnxlp, libnxsl, and libnxmap
[public/netxms.git] / src / libnxsl / main.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: main.cpp
21 **
22 **/
23
24 #include "libnxsl.h"
25
26 #ifdef _WIN32
27 #define read _read
28 #define close _close
29 #endif
30
31
32 //
33 // For unknown reasons, min() becames undefined on Linux, despite the fact
34 // that it is defined in nms_common.h
35 //
36
37 #ifndef min
38 #define min(a, b) ((a) < (b) ? (a) : (b))
39 #endif
40
41
42 //
43 // Interface to compiler
44 //
45
46 NXSL_Program LIBNXSL_EXPORTABLE *NXSLCompile(const TCHAR *pszSource,
47 TCHAR *pszError, int nBufSize)
48 {
49 NXSL_Compiler compiler;
50 NXSL_Program *pResult;
51
52 pResult = compiler.compile(pszSource);
53 if (pResult == NULL)
54 {
55 if (pszError != NULL)
56 nx_strncpy(pszError, compiler.getErrorText(), nBufSize);
57 }
58 return pResult;
59 }
60
61
62 //
63 // Load file into memory
64 //
65
66 TCHAR LIBNXSL_EXPORTABLE *NXSLLoadFile(const TCHAR *pszFileName, DWORD *pdwFileSize)
67 {
68 int fd, iBufPos, iNumBytes, iBytesRead;
69 TCHAR *pBuffer = NULL;
70 struct stat fs;
71
72 fd = _topen(pszFileName, O_RDONLY | O_BINARY);
73 if (fd != -1)
74 {
75 if (fstat(fd, &fs) != -1)
76 {
77 pBuffer = (TCHAR *)malloc(fs.st_size + 1);
78 if (pBuffer != NULL)
79 {
80 *pdwFileSize = fs.st_size;
81 for(iBufPos = 0; iBufPos < fs.st_size; iBufPos += iBytesRead)
82 {
83 iNumBytes = min(16384, fs.st_size - iBufPos);
84 if ((iBytesRead = read(fd, &pBuffer[iBufPos], iNumBytes)) < 0)
85 {
86 free(pBuffer);
87 pBuffer = NULL;
88 break;
89 }
90 }
91 if (pBuffer != NULL)
92 {
93 for(iBufPos = 0; iBufPos < fs.st_size; iBufPos++)
94 if (pBuffer[iBufPos] == 0)
95 pBuffer[iBufPos] = ' ';
96 pBuffer[fs.st_size] = 0;
97 }
98 }
99 }
100 close(fd);
101 }
102 return pBuffer;
103 }
104
105
106 //
107 // DLL entry point
108 //
109
110 #ifdef _WIN32
111
112 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
113 {
114 if (dwReason == DLL_PROCESS_ATTACH)
115 DisableThreadLibraryCalls(hInstance);
116 return TRUE;
117 }
118
119 #endif /* _WIN32 */