| 1 | /* $Id: ImpDef.cpp,v 1.6 2000-12-17 03:26:41 bird Exp $ */
|
|---|
| 2 | /*
|
|---|
| 3 | * ImpDef - Create export file which use internal names and ordinals.
|
|---|
| 4 | *
|
|---|
| 5 | * Copyright (c) 1999 knut st. osmundsen
|
|---|
| 6 | *
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | /*******************************************************************************
|
|---|
| 10 | * Header Files *
|
|---|
| 11 | *******************************************************************************/
|
|---|
| 12 | #include <os2.h>
|
|---|
| 13 | #include <stdio.h>
|
|---|
| 14 | #include <string.h>
|
|---|
| 15 | #include <stdlib.h>
|
|---|
| 16 | #include "ImpDef.h"
|
|---|
| 17 | #include "kFile.h"
|
|---|
| 18 | #include "kFileFormatBase.h"
|
|---|
| 19 | #include "kFileDef.h"
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | /*******************************************************************************
|
|---|
| 23 | * Internal Functions *
|
|---|
| 24 | *******************************************************************************/
|
|---|
| 25 | static void syntax(void);
|
|---|
| 26 | static long processFile(const char *pszInput, const char *pszOutput, const POPTIONS pOptions);
|
|---|
| 27 | static char *generateExportName(const PEXPORTENTRY pExport, char *pszBuffer, const POPTIONS pOptions);
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Main function.
|
|---|
| 32 | * @returns 0 on success.
|
|---|
| 33 | * @param argc Number of arguments
|
|---|
| 34 | * @param argv Array of arguments
|
|---|
| 35 | */
|
|---|
| 36 | int main(int argc, char **argv)
|
|---|
| 37 | {
|
|---|
| 38 | int argi;
|
|---|
| 39 | BOOL fFatal = FALSE;
|
|---|
| 40 | long lRc = 0;
|
|---|
| 41 | char *pszInput = NULL;
|
|---|
| 42 | char *pszOutput = NULL;
|
|---|
| 43 | OPTIONS options = {TRUE, ORD_START_INTERNAL_FUNCTIONS, FALSE, TRUE};
|
|---|
| 44 |
|
|---|
| 45 | /**************************************************************************
|
|---|
| 46 | * parse arguments.
|
|---|
| 47 | * options: -h or -? help
|
|---|
| 48 | * -S<[+]|-> Similar to exported. (stdcall)
|
|---|
| 49 | * -O<[+]|-> Remove OS2 prefix on APIs.
|
|---|
| 50 | * -I:<num> Start ordinal number of internal functions.
|
|---|
| 51 | * -F<[+]|-> Export intname for internal stdcall functions.
|
|---|
| 52 | **************************************************************************/
|
|---|
| 53 | if (argc == 1)
|
|---|
| 54 | syntax();
|
|---|
| 55 | argi = 1;
|
|---|
| 56 | while (argi < argc && !fFatal)
|
|---|
| 57 | {
|
|---|
| 58 | if(argv[argi][0] == '-' || argv[argi][0] == '/')
|
|---|
| 59 | {
|
|---|
| 60 | switch (argv[argi][1])
|
|---|
| 61 | {
|
|---|
| 62 | case 's':
|
|---|
| 63 | case 'S':
|
|---|
| 64 | options.fSimilarToExported = argv[argi][2] != '-';
|
|---|
| 65 | break;
|
|---|
| 66 |
|
|---|
| 67 | case 'o':
|
|---|
| 68 | case 'O':
|
|---|
| 69 | options.fRemoveOS2APIPrefix = argv[argi][2] != '-';
|
|---|
| 70 | break;
|
|---|
| 71 |
|
|---|
| 72 | case 'i':
|
|---|
| 73 | case 'I':
|
|---|
| 74 | if (strlen(argv[argi]) >= 3)
|
|---|
| 75 | {
|
|---|
| 76 | options.ulOrdStartInternalFunctions = atol(&argv[argi][3]);
|
|---|
| 77 | if (options.ulOrdStartInternalFunctions == 0)
|
|---|
| 78 | kFile::StdErr.printf("warning: internal functions starts at ordinal 0!\n");
|
|---|
| 79 | }
|
|---|
| 80 | else
|
|---|
| 81 | {
|
|---|
| 82 | kFile::StdErr.printf("incorrect parameter -I:<ord>. (argi=%d, argv[argi]=%s)\n", argi, argv[argi]);
|
|---|
| 83 | fFatal = TRUE;
|
|---|
| 84 | }
|
|---|
| 85 | break;
|
|---|
| 86 |
|
|---|
| 87 | case 'f':
|
|---|
| 88 | case 'F':
|
|---|
| 89 | options.fInternalFnExportStdCallsIntName = argv[argi][2] != '-';
|
|---|
| 90 | break;
|
|---|
| 91 |
|
|---|
| 92 | case '?':
|
|---|
| 93 | case 'h':
|
|---|
| 94 | case 'H':
|
|---|
| 95 | syntax();
|
|---|
| 96 | return 0;
|
|---|
| 97 |
|
|---|
| 98 | default:
|
|---|
| 99 | kFile::StdErr.printf("incorrect parameter. (argi=%d, argv[argi]=%s)\n", argi, argv[argi]);
|
|---|
| 100 | fFatal = TRUE;
|
|---|
| 101 | break;
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | else
|
|---|
| 105 | {
|
|---|
| 106 | if (pszInput == NULL)
|
|---|
| 107 | pszInput = argv[argi];
|
|---|
| 108 | else if (pszOutput == NULL)
|
|---|
| 109 | pszOutput = argv[argi];
|
|---|
| 110 | else
|
|---|
| 111 | {
|
|---|
| 112 | kFile::StdErr.printf("To many files are specified!\n");
|
|---|
| 113 | fFatal = TRUE;
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 | argi++;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | if (pszInput == NULL)
|
|---|
| 120 | {
|
|---|
| 121 | fFatal = TRUE;
|
|---|
| 122 | kFile::StdErr.printf("Missing input file.\n");
|
|---|
| 123 | }
|
|---|
| 124 | else if (pszOutput == NULL)
|
|---|
| 125 | {
|
|---|
| 126 | fFatal = TRUE;
|
|---|
| 127 | kFile::StdErr.printf("Missing output file.\n");
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | if (!fFatal)
|
|---|
| 131 | lRc = processFile(pszInput, pszOutput, &options);
|
|---|
| 132 | else
|
|---|
| 133 | lRc = -1;
|
|---|
| 134 |
|
|---|
| 135 | return (int)lRc;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * Print syntax/help message.
|
|---|
| 141 | */
|
|---|
| 142 | static void syntax(void)
|
|---|
| 143 | {
|
|---|
| 144 | kFile::StdOut.printf(
|
|---|
| 145 | "\n"
|
|---|
| 146 | "ImpDef - Creates internal import definition file\n"
|
|---|
| 147 | "------------------------------------------------\n"
|
|---|
| 148 | "syntax: ImpDef.exe [-h|-?] [-S] <infile> <outfile>\n"
|
|---|
| 149 | " -h or -? Syntax help. (this)\n"
|
|---|
| 150 | " -F<[+]|-> Fix! Export int.name for int.functions. default: F+\n"
|
|---|
| 151 | " -I:<ord> Start of internal function. default: I:%d\n"
|
|---|
| 152 | " -O<[+]|-> Remove OS2 prefix on APIs. default: O-\n"
|
|---|
| 153 | " -S<[+]|-> Similar to exported name. default: S+\n"
|
|---|
| 154 | " infile Name of input file\n"
|
|---|
| 155 | " outfile Name of output file\n"
|
|---|
| 156 | "\n"
|
|---|
| 157 | "Notes:\n"
|
|---|
| 158 | " -S+ only works on stdcall functions (having '@' in the internal name).\n"
|
|---|
| 159 | " -S+ takes the '_' and the '@..' parts from the internal name and adds it\n"
|
|---|
| 160 | " to the exported name. This way the OS2 prefix is removed.\n"
|
|---|
| 161 | " -O+ has no effect on stdcall functions when -S+ is set. -S+ has higher\n"
|
|---|
| 162 | " precedence than -O+.\n"
|
|---|
| 163 | " -O+ only removes the OS2 prefix from internal names.\n",
|
|---|
| 164 | ORD_START_INTERNAL_FUNCTIONS
|
|---|
| 165 | );
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | *
|
|---|
| 171 | * @returns 0 on success, error code on error.
|
|---|
| 172 | * @param pszInput Input filename.
|
|---|
| 173 | * @param pszOuput Output filename.
|
|---|
| 174 | * @param pOptions Pointer to options struct.
|
|---|
| 175 | * @sketch Open input file
|
|---|
| 176 | * try create a kFileDef object from inputfile.
|
|---|
| 177 | * Open output file.
|
|---|
| 178 | * Generate output file.
|
|---|
| 179 | * @remark
|
|---|
| 180 | */
|
|---|
| 181 | static long processFile(const char *pszInput, const char *pszOutput, const POPTIONS pOptions)
|
|---|
| 182 | {
|
|---|
| 183 | long lRc = 0;
|
|---|
| 184 |
|
|---|
| 185 | try
|
|---|
| 186 | {
|
|---|
| 187 | kFile Input(pszInput);
|
|---|
| 188 | try
|
|---|
| 189 | {
|
|---|
| 190 | kFileDef DefFile(&Input);
|
|---|
| 191 | try
|
|---|
| 192 | {
|
|---|
| 193 | EXPORTENTRY export;
|
|---|
| 194 | kFile Output(pszOutput, FALSE);
|
|---|
| 195 |
|
|---|
| 196 | /* generate LIBRARY line */
|
|---|
| 197 | Output.printf(
|
|---|
| 198 | ";Internal export definition file - autogenerated by ImpDef.\n"
|
|---|
| 199 | "%s\n",
|
|---|
| 200 | DefFile.queryType());
|
|---|
| 201 |
|
|---|
| 202 | /* Description line */
|
|---|
| 203 | if (DefFile.queryDescription())
|
|---|
| 204 | Output.printf("DESCRIPTION '%s'\n", DefFile.queryDescription());
|
|---|
| 205 |
|
|---|
| 206 | /* Exports */
|
|---|
| 207 | if (DefFile.findFirstExport(&export))
|
|---|
| 208 | {
|
|---|
| 209 | Output.printf("EXPORTS\n");
|
|---|
| 210 | do
|
|---|
| 211 | {
|
|---|
| 212 | char szName[MAXEXPORTNAME];
|
|---|
| 213 | const char *pszName;
|
|---|
| 214 |
|
|---|
| 215 | /* validate export struct */
|
|---|
| 216 | if (export.achName[0] == '\0')
|
|---|
| 217 | {
|
|---|
| 218 | kFile::StdErr.printf(
|
|---|
| 219 | "Warning export name is missing.\n"
|
|---|
| 220 | "info:\texport.achIntName=%s\n\texport.achName=%s\n\texport.ulOrdinal=%ld\n",
|
|---|
| 221 | export.achIntName, export.achName, export.ulOrdinal);
|
|---|
| 222 | continue;
|
|---|
| 223 | }
|
|---|
| 224 | if (export.ulOrdinal == ~0UL)
|
|---|
| 225 | {
|
|---|
| 226 | kFile::StdErr.printf(
|
|---|
| 227 | "warning: export is missing ordinal value. Export is ignored\n"
|
|---|
| 228 | "info:\texport.achIntName=%s\n\texport.achName=%s\n\texport.ulOrdinal=%ld\n",
|
|---|
| 229 | export.achIntName, export.achName, export.ulOrdinal);
|
|---|
| 230 | continue;
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /* real work */
|
|---|
| 234 | pszName = generateExportName(&export, &szName[0], pOptions);
|
|---|
| 235 |
|
|---|
| 236 | Output.printf(" %-*s @%ld\n", 40, pszName, export.ulOrdinal);
|
|---|
| 237 | } while (DefFile.findNextExport(&export));
|
|---|
| 238 | Output.setSize();
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 | catch (int errorcode)
|
|---|
| 242 | {
|
|---|
| 243 | kFile::StdErr.printf("error creating output file, '%s', errorcode 0x%x\n", pszOutput, errorcode);
|
|---|
| 244 | lRc = -4;
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 | catch (int errorcode)
|
|---|
| 248 | {
|
|---|
| 249 | kFile::StdErr.printf("%s is not a valid def file, errorcode 0x%x\n", pszInput, errorcode);
|
|---|
| 250 | lRc = -3;
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 | catch (int errorcode)
|
|---|
| 254 | {
|
|---|
| 255 | kFile::StdErr.printf( "error openining inputfile, '%s', errorcode 0x%x\n", pszInput, errorcode);
|
|---|
| 256 | lRc = -2;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | return lRc;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 | /**
|
|---|
| 264 | * Generate export names according to options defines.
|
|---|
| 265 | * fSimilarToExported only applies to stdcall API functions.
|
|---|
| 266 | * fRemoveOS2APIPrefix only applies to APIs.
|
|---|
| 267 | * fRemoveOS2APIPrefix have no effect on stdcall functions when fSimilarToExported is set.
|
|---|
| 268 | * fRemoveOS2APIPrefix only applies to the internal names.
|
|---|
| 269 | * @returns Pointer to buffer.
|
|---|
| 270 | * @param pExport Export entry.
|
|---|
| 271 | * @param pszBuffer Pointer to a string buffer which the result is returned in.
|
|---|
| 272 | * @param pOptions Pointer to options-struct.
|
|---|
| 273 | * @precond The export data (pExport) is valiaded.
|
|---|
| 274 | * @sketch write only code... but it works (I hope).
|
|---|
| 275 | * @remark
|
|---|
| 276 | */
|
|---|
| 277 | static char *generateExportName(const PEXPORTENTRY pExport, char *pszBuffer, const POPTIONS pOptions)
|
|---|
| 278 | {
|
|---|
| 279 | if (pExport->ulOrdinal < pOptions->ulOrdStartInternalFunctions)
|
|---|
| 280 | {
|
|---|
| 281 | /* API */
|
|---|
| 282 | if (pOptions->fSimilarToExported)
|
|---|
| 283 | {
|
|---|
| 284 | if (pExport->achIntName[0] != '\0')
|
|---|
| 285 | {
|
|---|
| 286 | char *pszAt = strchr(&pExport->achIntName[0], '@');
|
|---|
| 287 | if (pszAt != NULL && pExport->achIntName[0] == '_' && pExport->achName[0] != '"')
|
|---|
| 288 | { /* stdcall - merge */
|
|---|
| 289 | strcpy(pszBuffer, "_");
|
|---|
| 290 | /* test for "reserved" definition file names (like HeapSize) in original def-file. */
|
|---|
| 291 | if (pExport->achName[0] != '"')
|
|---|
| 292 | strcat(pszBuffer, &pExport->achName[0]);
|
|---|
| 293 | else
|
|---|
| 294 | {
|
|---|
| 295 | strcat(pszBuffer, &pExport->achName[1]);
|
|---|
| 296 | pszBuffer[strlen(pszBuffer)-1] = '\0'; //remove tail '"'
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | strcat(pszBuffer, pszAt);
|
|---|
| 300 | }
|
|---|
| 301 | else
|
|---|
| 302 | { /* not a stdcall - no merge but check for OS2prefix */
|
|---|
| 303 | if (pOptions->fRemoveOS2APIPrefix)
|
|---|
| 304 | {
|
|---|
| 305 | int i = 0;
|
|---|
| 306 | char *psz = pszBuffer;
|
|---|
| 307 | if (pExport->achIntName[i] == '_')
|
|---|
| 308 | *psz++ = pExport->achIntName[i++];
|
|---|
| 309 | if (strncmp(&pExport->achIntName[i], "OS2", 3) == 0)
|
|---|
| 310 | i += 3;
|
|---|
| 311 | strcpy(psz, &pExport->achIntName[i]);
|
|---|
| 312 | }
|
|---|
| 313 | else
|
|---|
| 314 | strcpy(pszBuffer, &pExport->achIntName[0]);
|
|---|
| 315 | }
|
|---|
| 316 | }
|
|---|
| 317 | else
|
|---|
| 318 | strcpy(pszBuffer, &pExport->achName[0]);
|
|---|
| 319 | }
|
|---|
| 320 | else if (pOptions->fRemoveOS2APIPrefix)
|
|---|
| 321 | { /* removes OS2 prefix */
|
|---|
| 322 | if (pExport->achIntName[0] != '\0')
|
|---|
| 323 | {
|
|---|
| 324 | int i = 0;
|
|---|
| 325 | char *psz = pszBuffer;
|
|---|
| 326 | if (pExport->achIntName[i] == '_')
|
|---|
| 327 | *psz++ = pExport->achIntName[i++];
|
|---|
| 328 | if (strncmp(&pExport->achIntName[i], "OS2", 3) == 0)
|
|---|
| 329 | i += 3;
|
|---|
| 330 | strcpy(psz, &pExport->achIntName[i]);
|
|---|
| 331 | }
|
|---|
| 332 | else
|
|---|
| 333 | strcpy(pszBuffer, &pExport->achName[0]);
|
|---|
| 334 | }
|
|---|
| 335 | else
|
|---|
| 336 | if (pExport->achIntName[0] != '\0')
|
|---|
| 337 | strcpy(pszBuffer, &pExport->achIntName[0]);
|
|---|
| 338 | else
|
|---|
| 339 | strcpy(pszBuffer, &pExport->achName[0]);
|
|---|
| 340 | }
|
|---|
| 341 | else
|
|---|
| 342 | { /* non-API functions */
|
|---|
| 343 | if ((pExport->achName[0] == '\0' || (pOptions->fInternalFnExportStdCallsIntName && strchr(&pExport->achIntName[0], '@')))
|
|---|
| 344 | && pExport->achIntName[0] != '\0'
|
|---|
| 345 | )
|
|---|
| 346 | strcpy(pszBuffer, &pExport->achIntName[0]);
|
|---|
| 347 | else
|
|---|
| 348 | strcpy(pszBuffer, &pExport->achName[0]);
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | return pszBuffer;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|