source: trunk/src/win32k/pe2lx/pe2lxmain.cpp@ 5050

Last change on this file since 5050 was 5050, checked in by bird, 25 years ago
All-in-one-object fix is now implmented thru an option. (-1<+|[*]>)
File size: 9.9 KB
Line 
1/* $Id: pe2lxmain.cpp,v 1.6 2001-02-02 08:35:54 bird Exp $
2 *
3 * Pe2Lx main program. (Ring 3 only!)
4 *
5 * Copyright (c) 1999 knut st. osmundsen
6 *
7 * Project Odin Software License can be found in LICENSE.TXT
8 *
9 */
10
11/*******************************************************************************
12* Defined Constants And Macros *
13*******************************************************************************/
14#define FOR_EXEHDR 1
15#define INCL_BASE
16#define INCL_DOSFILEMGR
17#define INCL_DOSERRORS
18
19#define DATA16_GLOBAL
20#define OUTPUT_COM2 0x2f8
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#include <os2.h>
26#include <peexe.h>
27#include <neexe.h>
28#include <newexe.h>
29#include <exe386.h>
30#include <malloc.h>
31#include <string.h>
32#include <stdlib.h>
33#include "OS2Krnl.h"
34#include "modulebase.h"
35#include "pe2lx.h"
36#include "options.h"
37#include <stdio.h>
38#include <versionos2.h>
39
40
41/*******************************************************************************
42* Global Variables *
43*******************************************************************************/
44char szBackupWin32Filename[CCHMAXPATH]; /* too save stack/heap */
45struct options options = DEFAULT_OPTION_ASSIGMENTS;
46
47/*******************************************************************************
48* Internal Functions *
49*******************************************************************************/
50static void syntax();
51
52
53/**
54 * Main function of the Pe2Lx utillity.
55 * @returns 0 on success.
56 * 1 Help.
57 * 2 Syntax error: Invalid argument, '<arg>'.
58 * 3 Syntax error: Too many filenames.
59 * 4 Syntax error: No Win32-file specified.
60 * 80 Fatal error: Can't find Win32-file, <filename>.
61 * 81 Fatal error: Failed to rename the Win32-file, <from> -> <to>
62 * 82 Fatal error: Failed to open Win32-file, <filename>. rc=<rc>
63 * 83 Fatal error: Failed to convertert the file. rc=<rc from init(..)>
64 * 84 Fatal error: Failed to write the Lx-file. rc=<rc from writeFile(..)>
65 * @param argc Count of arguments.
66 * @param argv Array of argument pointers. argc entries.
67 * @status completely implemented.
68 * @author knut st. osmundsen
69 */
70int main(int argc, char **argv)
71{
72 APIRET rc;
73 ULONG ulAction = 0;
74 PCSZ pszWin32Filename = NULL;
75 PCSZ pszOdin32Filename = NULL;
76 PCSZ psz;
77 int argi;
78
79 /* read parameters */
80 for (argi = 1; argi < argc; argi++)
81 {
82 /* check if option or filname */
83 if (argv[argi][0] == '-' || argv[argi][0] == '/')
84 { /* option */
85 switch (argv[argi][1])
86 {
87 case '1': /* All-In-One-Object fix - temporary...- -1<-|+|*> */
88 if (argv[argi][2] == '-')
89 options.fPEOneObject = FLAGS_PEOO_DISABLED;
90 else if (argv[argi][2] == '+')
91 options.fPEOneObject = FLAGS_PEOO_ENABLED;
92 else
93 options.fPEOneObject = FLAGS_PEOO_FORCED;
94 break;
95
96
97 case 'h': /* syntax help */
98 case 'H':
99 case '?':
100 syntax();
101 return 1;
102
103 case 'w': /* warning level */
104 case 'W':
105 psz = argv[argi] + (argv[argi][2] == ':' || argv[argi][2] == '=') + 2;
106 if (*psz >= '0' && *psz <= '4' && psz[1] == '\0')
107 {
108 switch (*psz)
109 {
110 case '0': ModuleBase::ulInfoLevel = ModuleBase::Quiet; break;
111 case '1': ModuleBase::ulInfoLevel = ModuleBase::Error; break;
112 case '2': ModuleBase::ulInfoLevel = ModuleBase::Warning; break;
113 case '3': ModuleBase::ulInfoLevel = ModuleBase::Info; break;
114 case '4': ModuleBase::ulInfoLevel = ModuleBase::InfoAll; break;
115 }
116 }
117 else
118 {
119 printf("Syntax error: Incorrect use of argument '%.2s'.\n\n", argv[argi]);
120 return 5;
121 }
122 break;
123
124 default:
125 printf("Syntax error: Invalid argument, '%s'\n", argv[argi]);
126 syntax();
127 return 2;
128 }
129 }
130 else
131 {
132 if (pszWin32Filename == NULL)
133 {
134 pszWin32Filename = argv[argi];
135 /* check if exists */
136 rc = DosQueryPathInfo(pszWin32Filename,FIL_QUERYFULLNAME,
137 &szBackupWin32Filename[0], sizeof(szBackupWin32Filename));
138 if (rc != NO_ERROR)
139 {
140 printf("Fatal error: Can't find Win32-file, '%s'.\n", pszWin32Filename);
141 return 80;
142 }
143 }
144 else if (pszOdin32Filename == NULL)
145 pszOdin32Filename = argv[argi];
146 else
147 {
148 printf("Syntax error: Too many filenames!\n");
149 syntax();
150 return 3;
151 }
152 }
153 } /* for */
154
155 /* check if enough arguments */
156 if (pszWin32Filename == NULL)
157 {
158 printf("Syntax error: No Win32-file specified.\n\n");
159 syntax();
160 return 4;
161 }
162
163 /* rename files? */
164 if (pszOdin32Filename == NULL)
165 {
166 char *pszExt = strrchr(pszWin32Filename, '.');
167 if (pszExt == NULL)
168 {
169 printf("warning: Win32-file don't have an extention\n");
170 strcpy(szBackupWin32Filename, pszWin32Filename);
171 strcat(szBackupWin32Filename, "wbk");
172 }
173 else
174 {
175 memset(szBackupWin32Filename, 0, sizeof(szBackupWin32Filename));
176 if (stricmp(pszExt + 1, "exe") == 0)
177 {
178 strncpy(szBackupWin32Filename, pszWin32Filename, pszExt - pszWin32Filename + 1);
179 strcat(szBackupWin32Filename, "exf");
180 }
181 else
182 {
183 strncpy(szBackupWin32Filename, pszWin32Filename, pszExt - pszWin32Filename + 3);
184 strcat(szBackupWin32Filename, "k");
185 }
186 }
187 rc = DosMove(pszWin32Filename, szBackupWin32Filename);
188 if (rc != NO_ERROR)
189 {
190 printf("Fatal error: Failed to rename the Win32-file, %s -> %s\n",
191 pszWin32Filename, szBackupWin32Filename);
192 return 81;
193 }
194 printInf(("Backuped %s to %s\n", pszWin32Filename, szBackupWin32Filename));
195
196 /* switch name */
197 pszOdin32Filename = pszWin32Filename;
198 pszWin32Filename = szBackupWin32Filename;
199 }
200
201 /* open input file */
202 HFILE hFileWin32;
203
204 rc = DosOpen(pszWin32Filename, &hFileWin32, &ulAction, 0UL,
205 FILE_NORMAL,
206 OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
207 OPEN_FLAGS_RANDOMSEQUENTIAL | OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
208 NULL);
209 if (rc == NO_ERROR)
210 {
211 Pe2Lx pe2lx(hFileWin32);
212 rc = pe2lx.init(pszOdin32Filename);
213 if (rc == NO_ERROR)
214 {
215 rc = pe2lx.writeFile(pszOdin32Filename);
216 if (rc != NO_ERROR)
217 {
218 printf("Fatal error: Failed to write the Lx-file. rc=%d\n", rc);
219 rc = 84;
220 }
221 pe2lx.dumpVirtualLxFile();
222 }
223 else
224 {
225 printf("Fatal error: Failed to convertert the file. rc=%d\n", rc);
226 rc = 83;
227 }
228 DosClose(hFileWin32);
229 }
230 else
231 {
232 printf("Fatal error: Failed to open Win32-file, %s. rc=%d\n",
233 pszWin32Filename, rc);
234 rc = 82;
235 }
236 return (int)rc;
237}
238
239
240/**
241 * Display syntax for this program.
242 * @status completely implemented.
243 * @author knut st. osmundsen
244 */
245static void syntax()
246{
247 printf("Syntax: pe2lx.exe [-W<0|1|2|3>] [-1<+|-|[*]>] <Win32File> [Odin32File]\n"
248 "\n"
249 " -W<0|1|2|3|4> Message filter level.\n"
250 " -W0: Output only severe and unrecoverable error messages.\n"
251 " -W1: Output error, severe and unrecoverable error messages.\n"
252 " -W2: Output warning, error, severe and unrecoverable error\n"
253 " messages.\n"
254 " -W3: Output nearly all messages.\n"
255 " -W4: Output absolutely all messages.\n"
256 " Default: -W3\n"
257 " -1<+|-|[*]> All-in-one-object fix.\n"
258 " +: Fix applied when necessary.\n"
259 " -: Disabled. Never applied.\n"
260 " *: Forced. Applied every time.\n"
261 " Default: -1*\n"
262 " Win32File Input Win32 Exe, Dll or other Win32 PE file.\n"
263 " Odin32File Output Odin32-file. If not specified the Win32-file is\n"
264 " renamed and the Odin32-file will use the original name\n"
265 " of the Win32-file.\n"
266 " Pe2Lx version 0.%02d\n",
267 PE2LX_VERSION
268 );
269}
270
271
272
273#if 0
274/**
275 * Debug - see how much of the stack that have been used.
276 * Padd stack, and look in the debug storage view on program end.
277 * @param
278 * @status
279 * @author knut st. osmundsen
280 */
281static void initStack()
282{
283 PTIB pTib;
284 PPIB pPib;
285
286 DosGetInfoBlocks(&pTib, &pPib);
287 memset((PVOID)pTib->tib_pstack, 'k', (size_t)&pTib - 0x30 - (size_t)pTib->tib_pstack);
288}
289#endif
Note: See TracBrowser for help on using the repository browser.