1 | /* ***** BEGIN LICENSE BLOCK ***** |
---|
2 | * Version: CDDL 1.0/LGPL 2.1 |
---|
3 | * |
---|
4 | * The contents of this file are subject to the COMMON DEVELOPMENT AND |
---|
5 | * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use |
---|
6 | * this file except in compliance with the License. You may obtain a copy of |
---|
7 | * the License at http://www.sun.com/cddl/ |
---|
8 | * |
---|
9 | * Software distributed under the License is distributed on an "AS IS" basis, |
---|
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
---|
11 | * for the specific language governing rights and limitations under the |
---|
12 | * License. |
---|
13 | * |
---|
14 | * The Initial Developer of the Original Code is |
---|
15 | * Eugene Romanenko, netlabs.org. |
---|
16 | * Portions created by the Initial Developer are Copyright (C) 2006 |
---|
17 | * the Initial Developer. All Rights Reserved. |
---|
18 | * |
---|
19 | * Contributor(s): |
---|
20 | * |
---|
21 | * Alternatively, the contents of this file may be used under the terms of |
---|
22 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
---|
23 | * in which case the provisions of the LGPL are applicable instead of those |
---|
24 | * above. If you wish to allow use of your version of this file only under the |
---|
25 | * terms of the LGPL, and not to allow others to use your version of this file |
---|
26 | * under the terms of the CDDL, indicate your decision by deleting the |
---|
27 | * provisions above and replace them with the notice and other provisions |
---|
28 | * required by the LGPL. If you do not delete the provisions above, a recipient |
---|
29 | * may use your version of this file under the terms of any one of the CDDL |
---|
30 | * or the LGPL. |
---|
31 | * |
---|
32 | * ***** END LICENSE BLOCK ***** */ |
---|
33 | |
---|
34 | |
---|
35 | #define INCL_DOS |
---|
36 | #include <os2.h> |
---|
37 | |
---|
38 | #include "pluginman.h" |
---|
39 | #include "luutils.h" |
---|
40 | #include <dos.h> |
---|
41 | #include <fcntl.h> |
---|
42 | #include <stdio.h> |
---|
43 | #include <ludoc.xh> |
---|
44 | |
---|
45 | |
---|
46 | PluginManager::PluginManager() |
---|
47 | { |
---|
48 | plugins = new PluginInfoList; |
---|
49 | |
---|
50 | // Find exe path |
---|
51 | char buffer[ _MAX_PATH ]; |
---|
52 | char path[ _MAX_PATH ]; |
---|
53 | char drive[ _MAX_DRIVE ]; |
---|
54 | char dir[ _MAX_DIR ]; |
---|
55 | _splitpath( __argv[0], drive, dir, NULL, NULL ); |
---|
56 | _makepath( path, drive, dir, NULL, NULL ); |
---|
57 | strcpy( buffer, path ); |
---|
58 | strcat( buffer, "LU*.DLL" ); |
---|
59 | |
---|
60 | // enum plugins, (LU*.DLL) except for LUDOC.DLL, which is 'null' plugin |
---|
61 | // and Lucide.dll, which isn't plugin. |
---|
62 | struct find_t ffblk; |
---|
63 | unsigned done = _dos_findfirst( buffer, _A_RDONLY | _A_NORMAL, &ffblk ); |
---|
64 | while ( done == 0 ) |
---|
65 | { |
---|
66 | if ( ( stricmp( ffblk.name, "LUDOC.DLL" ) != 0 ) && |
---|
67 | ( stricmp( ffblk.name, "LUCIDE.DLL" ) != 0 ) ) |
---|
68 | { |
---|
69 | loadPlugin( path, ffblk.name ); |
---|
70 | } |
---|
71 | done = _dos_findnext( &ffblk ); |
---|
72 | } |
---|
73 | _dos_findclose( &ffblk ); |
---|
74 | } |
---|
75 | |
---|
76 | PluginManager::~PluginManager() |
---|
77 | { |
---|
78 | for ( int i = 0; i < plugins->size(); i++ ) |
---|
79 | { |
---|
80 | PluginInfo *pi = &(*plugins)[ i ]; |
---|
81 | DosFreeModule( pi->handle ); |
---|
82 | //printf( "NAME: %s EXT: %s DESC: %s\n", pi->name.c_str(), |
---|
83 | // pi->extensions.c_str(), pi->description.c_str() ); |
---|
84 | } |
---|
85 | delete plugins; |
---|
86 | } |
---|
87 | |
---|
88 | void PluginManager::loadPlugin( const char *path, const char *dllname ) |
---|
89 | { |
---|
90 | // Function pointer variables |
---|
91 | LuDocument * APIENTRY (*pCreateObject)() = NULL; |
---|
92 | char * APIENTRY (*pGetSupportedExtensions)() = NULL; |
---|
93 | char * APIENTRY (*pGetDescription)() = NULL; |
---|
94 | LuCheckStruct * APIENTRY (*pGetCheckStruct)() = NULL; |
---|
95 | |
---|
96 | std::string fulldllname = path; |
---|
97 | fulldllname += dllname; |
---|
98 | |
---|
99 | // cut DLL name at last point |
---|
100 | char *lpoint = strrchr( dllname, '.' ); |
---|
101 | *lpoint = '\0'; |
---|
102 | |
---|
103 | // load a DLL |
---|
104 | HMODULE h = NULLHANDLE; |
---|
105 | bool res = false; |
---|
106 | do |
---|
107 | { |
---|
108 | if ( DosLoadModule( NULL, 0, fulldllname.c_str(), &h ) != 0 ) |
---|
109 | break; |
---|
110 | if ( DosQueryProcAddr( h, 0, "createObject", (PFN *)&pCreateObject ) != 0 ) |
---|
111 | break; |
---|
112 | if ( DosQueryProcAddr( h, 0, "getSupportedExtensions", (PFN *)&pGetSupportedExtensions ) != 0 ) |
---|
113 | break; |
---|
114 | if ( DosQueryProcAddr( h, 0, "getDescription", (PFN *)&pGetDescription ) != 0 ) |
---|
115 | break; |
---|
116 | |
---|
117 | // optional |
---|
118 | if ( DosQueryProcAddr( h, 0, "getCheckStruct", (PFN *)&pGetCheckStruct ) != 0 ) { |
---|
119 | pGetCheckStruct = NULL; |
---|
120 | } |
---|
121 | |
---|
122 | res = true; |
---|
123 | } while (0); |
---|
124 | |
---|
125 | if ( res ) |
---|
126 | { |
---|
127 | PluginInfo pi; |
---|
128 | pi.handle = h; |
---|
129 | pi.name = dllname; |
---|
130 | pi.extensions = pGetSupportedExtensions(); |
---|
131 | pi.description = pGetDescription(); |
---|
132 | pi.checkStruct = ( pGetCheckStruct == NULL ) ? NULL : pGetCheckStruct(); |
---|
133 | |
---|
134 | plugins->push_back( pi ); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | // returns NULL if not suitable plugin found |
---|
140 | // if checkOnly is true - just check if suitable plugin exist |
---|
141 | LuDocument *PluginManager::createDocumentForExt( const char *ext, bool checkOnly ) |
---|
142 | { |
---|
143 | if ( ext == NULL ) { |
---|
144 | return NULL; |
---|
145 | } |
---|
146 | |
---|
147 | LuDocument * APIENTRY (*pCreateObject)() = NULL; |
---|
148 | |
---|
149 | for ( int i = 0; i < plugins->size(); i++ ) |
---|
150 | { |
---|
151 | PluginInfo *pi = &(*plugins)[ i ]; |
---|
152 | |
---|
153 | char *cExt = new char[ strlen( ext ) + 5 ]; |
---|
154 | strcpy( cExt, ";" ); |
---|
155 | strcat( cExt, ext ); |
---|
156 | strcat( cExt, ";" ); |
---|
157 | strupr( cExt ); |
---|
158 | |
---|
159 | char *cExts = new char[ pi->extensions.length() + 5 ]; |
---|
160 | strcpy( cExts, ";" ); |
---|
161 | strcat( cExts, pi->extensions.c_str() ); |
---|
162 | strcat( cExts, ";" ); |
---|
163 | strupr( cExts ); |
---|
164 | |
---|
165 | if ( strstr( cExts, cExt ) != NULL ) |
---|
166 | { |
---|
167 | if ( DosQueryProcAddr( pi->handle, 0, "createObject", |
---|
168 | (PFN *)&pCreateObject ) == 0 ) |
---|
169 | { |
---|
170 | delete cExt; |
---|
171 | delete cExts; |
---|
172 | |
---|
173 | if ( checkOnly ) { |
---|
174 | return (LuDocument *)TRUE; |
---|
175 | } |
---|
176 | else { |
---|
177 | return pCreateObject(); |
---|
178 | } |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | delete cExt; |
---|
183 | delete cExts; |
---|
184 | } |
---|
185 | |
---|
186 | return NULL; |
---|
187 | } |
---|
188 | |
---|
189 | static bool checkDataEntry( LuCheckData *data, int h ) |
---|
190 | { |
---|
191 | lseek( h, data->offset, SEEK_SET ); |
---|
192 | unsigned char *buf = new char[ data->length ]; |
---|
193 | read( h, buf, data->length ); |
---|
194 | bool result = ( memcmp( data->data, buf, data->length ) == 0 ); |
---|
195 | delete buf; |
---|
196 | return result; |
---|
197 | } |
---|
198 | |
---|
199 | static bool checkData( LuCheckStruct *checkStruct, const char *file ) |
---|
200 | { |
---|
201 | int h = open( file, O_RDONLY | O_BINARY ); |
---|
202 | |
---|
203 | if ( h != -1 ) |
---|
204 | { |
---|
205 | bool result = true; |
---|
206 | |
---|
207 | for ( unsigned long i = 0; i < checkStruct->count; i++ ) |
---|
208 | { |
---|
209 | if ( !checkDataEntry( &(checkStruct->cdata[ i ]), h ) ) { |
---|
210 | result = false; |
---|
211 | break; |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | close( h ); |
---|
216 | return result; |
---|
217 | } |
---|
218 | |
---|
219 | return false; |
---|
220 | } |
---|
221 | |
---|
222 | // returns NULL if not suitable plugin found |
---|
223 | // if checkOnly is true - just check if suitable plugin exist |
---|
224 | LuDocument *PluginManager::createDocumentForFile( const char *file, bool checkOnly ) |
---|
225 | { |
---|
226 | LuDocument *ld = NULL; |
---|
227 | |
---|
228 | // Search by extension |
---|
229 | char *ext = strrchr( file, '.' ); |
---|
230 | if ( ext != NULL ) { |
---|
231 | ld = createDocumentForExt( ext + 1, checkOnly ); |
---|
232 | } |
---|
233 | |
---|
234 | if ( ld != NULL ) { |
---|
235 | return ld; |
---|
236 | } |
---|
237 | |
---|
238 | // Search by checkstruct |
---|
239 | LuDocument * APIENTRY (*pCreateObject)() = NULL; |
---|
240 | |
---|
241 | for ( int i = 0; i < plugins->size(); i++ ) |
---|
242 | { |
---|
243 | PluginInfo *pi = &(*plugins)[ i ]; |
---|
244 | |
---|
245 | if ( pi->checkStruct != NULL ) |
---|
246 | { |
---|
247 | if ( checkData( pi->checkStruct, file ) ) |
---|
248 | { |
---|
249 | if ( DosQueryProcAddr( pi->handle, 0, "createObject", |
---|
250 | (PFN *)&pCreateObject ) == 0 ) |
---|
251 | { |
---|
252 | if ( checkOnly ) { |
---|
253 | return (LuDocument *)TRUE; |
---|
254 | } |
---|
255 | else { |
---|
256 | return pCreateObject(); |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | return NULL; |
---|
264 | } |
---|
265 | |
---|
266 | std::string PluginManager::getExtsMask() |
---|
267 | { |
---|
268 | std::string cRet = ""; |
---|
269 | |
---|
270 | for ( int i = 0; i < plugins->size(); i++ ) |
---|
271 | { |
---|
272 | PluginInfo *pi = &(*plugins)[ i ]; |
---|
273 | char *tmpexts = newstrdup( pi->extensions.c_str() ); |
---|
274 | char *p = strtok( tmpexts, ";" ); |
---|
275 | while( p != NULL ) { |
---|
276 | cRet += "*."; |
---|
277 | cRet += p; |
---|
278 | cRet += ";"; |
---|
279 | p = strtok( NULL, ";" ); |
---|
280 | } |
---|
281 | } |
---|
282 | return cRet; |
---|
283 | } |
---|
284 | |
---|
285 | |
---|