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