source: trunk/tools/database/APIImport.cpp@ 830

Last change on this file since 830 was 830, checked in by bird, 26 years ago

Initial checkin of the database.

File size: 11.2 KB
Line 
1/* $Id: APIImport.cpp,v 1.1 1999-09-05 02:53:04 bird Exp $ */
2/*
3 *
4 * APIImport - imports a DLL or Dll-.def with functions into the Odin32 database.
5 *
6 * Copyright (c) 1999 knut st. osmundsen
7 *
8 */
9
10
11/*******************************************************************************
12* Header Files *
13*******************************************************************************/
14#include <os2.h>
15#include <stdio.h>
16#include <string.h>
17
18#include "APIImport.h"
19#include "kFileFormatBase.h"
20#include "kFilePE.h"
21#include "kFileDef.h"
22#include "db.h"
23
24
25/*******************************************************************************
26* Global Variables *
27*******************************************************************************/
28static FILE *phLog = NULL;
29
30/*******************************************************************************
31* Internal Functions *
32*******************************************************************************/
33static void syntax(void);
34static void openLog(void);
35static void closeLog(void);
36static long processFile(const char *pszFilename, const POPTIONS pOptions);
37
38
39/**
40 * Main function.
41 * @returns low word : number of errors
42 * high word: flags.
43 * @param argc Argument count.
44 * @param argv Argument array.
45 */
46int main(int argc, char **argv)
47{
48 int argi;
49 BOOL fFatal = FALSE;
50 long lRc = 0;
51 OPTIONS options = {0};
52 char *pszHost = "localhost";
53 char *pszDatabase = "Odin32";
54 char *pszUser = "root";
55 char *pszPasswd = "";
56
57 /**************************************************************************
58 * parse arguments.
59 * options: -h or -? help
60 * -o[+|-] ordinals, o- ignore them, o+ import them (def).
61 * -d:<dbname> Database name
62 * -p:<passwd> Password
63 * -u:<user> Userid
64 * -h:<host> Hostname/IP-address
65 **************************************************************************/
66 if (argc == 1)
67 syntax();
68 argi = 1;
69 while (argi < argc && !fFatal)
70 {
71 if(argv[argi][0] == '-' || argv[argi][0] == '/')
72 {
73 switch (argv[argi][1])
74 {
75 case 'd':
76 case 'D':
77 if (argv[argi][2] == ':')
78 pszDatabase = &argv[argi][3];
79 else
80 fprintf(stderr, "warning: option '-d:' requires database name.\n");
81 break;
82
83 case 'h':
84 case 'H':
85 if (argv[argi][2] == ':')
86 {
87 pszHost = &argv[argi][3];
88 break;
89 }
90 case '?':
91 syntax();
92 return 0;
93
94 case 'o': /* ordinals*/
95 case 'O':
96 options.fIgnoreOrdinals = argv[argi][2] != '-';
97 break;
98
99 case 'p':
100 case 'P':
101 if (argv[argi][2] == ':')
102 pszPasswd = &argv[argi][3];
103 else
104 fprintf(stderr, "warning: option '-p:' requires password.\n");
105 break;
106
107 case 'u':
108 case 'U':
109 if (argv[argi][2] == ':')
110 pszUser = &argv[argi][3];
111 else
112 fprintf(stderr, "error: option '-u:' requires userid.\n");
113 break;
114
115 default:
116 fprintf(stderr, "incorrect parameter. (argi=%d, argv[argi]=%s)\n", argi, argv[argi]);
117 fFatal = TRUE;
118 break;
119 }
120 }
121 else
122 {
123 long l;
124 if (phLog == NULL)
125 openLog();
126 /* try connect to db */
127 if (dbConnect(pszHost, pszUser, pszPasswd, pszDatabase))
128 {
129 l = processFile(argv[argi], &options);
130 lRc = ((lRc & 0xffff0000UL) | (l & 0xffff0000UL)) | ((lRc & 0x0000ffffUL) + l & 0x0000ffffUL);
131 dbDisconnect();
132 }
133 else
134 {
135 fprintf(phLog, "Could not connect to database (Odin32). \n\terror description: %s\n", dbGetLastErrorDesc());
136 l = 0x00010001;
137 lRc = ((lRc & 0xffff0000UL) | (l & 0xffff0000UL)) | ((lRc & 0x0000ffffUL) + l & 0x0000ffffUL);
138 break;
139 }
140 }
141 argi++;
142 }
143
144 /* close the log */
145 closeLog();
146
147 /* warn if error during processing. */
148 if (!fFatal && lRc > 0)
149 fprintf(stderr, "%s did occur during the processing. Please check the log file. lRc=0x%08lx\n",
150 (0x0000FFFFL & lRc) > 1 ? "Errors" : "An error", lRc);
151 return (int)lRc;
152}
153
154
155
156/**
157 * Display syntax/help .
158 */
159static void syntax()
160{
161 printf("\n"
162 "APIImport v%01d.%02d - Odin32 API Database utility\n"
163 "----------------------------------------------\n"
164 "syntax: APIImport.exe [-h|-?] [options] <dll/defnames>\n"
165 "\n"
166 " -h or -? Syntax help. (this)\n"
167 " -o[+|-] Ordinals, '+' imports (default), '-' ignores.\n"
168 " -h:<hostname> Database server hostname. default: localhost\n"
169 " -u:<username> Username on the server. default: root\n"
170 " -p:<password> Password. default: <empty>\n"
171 " -d:<database> Database to use. default: Odin32\n"
172 " dll/defnames List of DLL/Defs to process.\n"
173 "\n"
174 "Note. Options may be switched on and off between dlls.\n"
175 "\n"
176 "\n"
177 "This utility will insert or update the 'dll' and 'function' tables in the\n"
178 "database. It read a PE-DLL or a OS/2 Definition file (.Def). From the file\n"
179 "it imports the:\n"
180 "\t1) module name\n"
181 "\t2) function name\n"
182 "\t3) function ordinal (If not option 'o-' is specified.)\n"
183 "This utililty is not destructive but additive. That is, it would not remove any\n"
184 "rows in the database, only add or update them. On existing functions the\n"
185 "ordinal value will be updated when 'o+' is specified.\n"
186 "\n"
187 "Copyright (c) 1999 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)",
188 MAJOR_VER, MINOR_VER
189 );
190}
191
192
193/**
194 * Opens the log file.
195 * @remark If open fails stderr is used.
196 */
197static void openLog(void)
198{
199 if (phLog == NULL)
200 {
201 phLog = fopen("APIImport.Log", "w");
202 if (phLog == NULL)
203 {
204 fprintf(stderr,"error occured while opening log file - will use stderr instead.\n");
205 phLog = stderr;
206 }
207 }
208}
209
210
211/**
212 * Closes log file.
213 */
214static void closeLog(void)
215{
216 if (phLog != stderr && phLog != NULL)
217 fclose(phLog);
218}
219
220
221/**
222 * Processes a file using the given options. Transparently processes .Def's and PE DLLs
223 * @returns low word, number of errors.
224 * high word, flags (currently not flags are defined/used).
225 * @param pszFilename Pointer to the filename of the file which is to be processed.
226 * @param pOptions Pointer to the options-struct.
227 */
228static long processFile(const char *pszFilename, const POPTIONS pOptions)
229{
230 kFileFormatBase *pFile;
231 FILE *phFile;
232 long lRc = 1;
233
234 /* try open file */
235 phFile = fopen(pszFilename, "rb");
236 if (phFile != NULL)
237 {
238 try
239 {
240 char achDataBuffer[0x200];
241 char *pszModuleName = &achDataBuffer[0];
242 signed short sDll;
243
244 /* try create file objects */
245 try
246 {
247 pFile = new kFilePE(phFile);
248 }
249 catch (int i)
250 {
251 i = i;
252 pFile = new kFileDef(phFile);
253 }
254
255 if (pFile->queryModuleName(pszModuleName))
256 {
257 int cch = strlen(pszModuleName);
258
259 /* remove '.dll' */
260 if (cch > 4 && !stricmp(&pszModuleName[cch-4], ".dll"))
261 pszModuleName[cch-4] = '\0';
262 fprintf(phLog, "%s: modulename = %s\n", pszFilename, pszModuleName);
263
264 /* find or insert module name */
265 if ((sDll = dbCheckInsertDll(pszModuleName)) >= 0)
266 {
267 BOOL fOk;
268 EXPORTENTRY export;
269
270 lRc = 0;
271 fOk = pFile->findFirstExport(&export);
272 while (fOk)
273 {
274 /* check if name or not */
275 if (!pFile->isDef() || export.ulOrdinal < ORD_START_INTERNAL_FUNCTIONS)
276 {
277 if (export.achName[0] != '\0')
278 { /* name */
279 int iName = strncmp(&export.achName[0], "_OS2", 4) != 0 ? 0 : 4;
280 fprintf(phLog, "%s: %08ld %s\n", pszFilename, export.ulOrdinal, &export.achName[iName]);
281 fOk = dbInsertUpdateFunction(sDll, &export.achName[iName], export.ulOrdinal,
282 pOptions->fIgnoreOrdinals && export.ulOrdinal != 0xffffffffUL);
283 }
284 else
285 { /* ordinal only */
286 char szFn[50];
287
288 sprintf(&szFn[0], "Ordinal%04ld", export.ulOrdinal);
289 fprintf(phLog, "%s: %08ld %s\n", pszFilename, export.ulOrdinal, &szFn[0]);
290 if (export.ulOrdinal != 0xffffffffUL)
291 fOk = dbInsertUpdateFunction(sDll, &szFn[0], export.ulOrdinal, pOptions->fIgnoreOrdinals);
292 else
293 fprintf(phLog, "%s: error - invalid ordinal value!\n", pszFilename);
294 }
295 }
296
297 if (!fOk)
298 {
299 fprintf(phLog, "%s: error - dbInsertUpdateFunction failed.\n\terror description: %s \n",
300 pszFilename, dbGetLastErrorDesc());
301 lRc = 1;
302 }
303
304 /* next */
305 fOk = pFile->findNextExport(&export);
306 }
307
308 }
309 else
310 fprintf(phLog, "%s: error - could not find/insert module name (%s).\n", pszFilename, pszModuleName);
311 }
312 else
313 fprintf(phLog, "%s: error - could not get module name.\n", pszFilename);
314
315 delete(pFile);
316 }
317 catch (int err)
318 {
319 fprintf(phLog, "%s: error - could not map dll/def into memory, errorno=%d.\n", pszFilename, err);
320 }
321 fclose(phFile);
322 }
323 else
324 fprintf(phLog, "%s: error - could not open file.\n", pszFilename);
325
326 /* close db */
327 dbDisconnect();
328
329 return lRc;
330}
331
Note: See TracBrowser for help on using the repository browser.