Release 950109
[wine.git] / misc / exec.c
blobdf0bee6e4a82db89dc7477eadca1414b89522e2b
1 /*
2 * Windows Exec & Help
4 */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include "neexe.h"
11 #include "segmem.h"
12 #include "prototypes.h"
13 #include "dlls.h"
14 #include "windows.h"
15 #include "if1632.h"
16 #include "callback.h"
17 #include "library.h"
18 #include "ne_image.h"
19 #include "stddebug.h"
20 #include "debug.h"
22 #define HELP_CONTEXT 0x0001
23 #define HELP_QUIT 0x0002
24 #define HELP_INDEX 0x0003
25 #define HELP_CONTENTS 0x0003
26 #define HELP_HELPONHELP 0x0004
27 #define HELP_SETINDEX 0x0005
28 #define HELP_SETCONTENTS 0x0005
29 #define HELP_CONTEXTPOPUP 0x0008
30 #define HELP_FORCEFILE 0x0009
31 #define HELP_KEY 0x0101
32 #define HELP_COMMAND 0x0102
33 #define HELP_PARTIALKEY 0x0105
34 #define HELP_MULTIKEY 0x0201
35 #define HELP_SETWINPOS 0x0203
37 typedef struct {
38 WORD wEnvSeg;
39 LPSTR lpCmdLine;
40 LPVOID lpCmdShow;
41 DWORD dwReserved;
42 } PARAMBLOCK;
44 typedef BOOL (CALLBACK * LPFNWINMAIN)(HANDLE, HANDLE, LPSTR, int);
46 HANDLE CreateNewTask(HINSTANCE hInst);
48 #ifndef WINELIB
49 void InitializeLoadedNewDLLs(HINSTANCE hInst)
51 struct w_files * w;
52 struct w_files * wpnt;
53 int cs_reg, ds_reg, ip_reg;
54 int rv;
56 dprintf_exec(stddeb, "Initializing New DLLs\n");
59 * Initialize libraries
61 dprintf_exec(stddeb,
62 "InitializeLoadedNewDLLs() before searching hInst=%04X !\n", hInst);
63 w = wine_files;
64 while (w && w->hinstance != hInst) w = w->next;
65 if (w == NULL) return;
66 dprintf_exec(stddeb,"InitializeLoadedNewDLLs() // before InitLoop !\n");
67 for(wpnt = w; wpnt; wpnt = wpnt->next)
69 /*
70 * Is this a library?
72 if (wpnt->ne->ne_header->format_flags & 0x8000)
74 if (!(wpnt->ne->ne_header->format_flags & 0x0001))
76 /* Not SINGLEDATA */
77 fprintf(stderr, "Library is not marked SINGLEDATA\n");
78 exit(1);
81 ds_reg = wpnt->ne->selector_table[wpnt->ne->
82 ne_header->auto_data_seg-1].selector;
83 cs_reg = wpnt->ne->selector_table[wpnt->ne->ne_header->cs-1].selector;
84 ip_reg = wpnt->ne->ne_header->ip;
86 dprintf_exec(stddeb, "Initializing %s, cs:ip %04x:%04x, ds %04x\n",
87 wpnt->name, cs_reg, ip_reg, ds_reg);
89 rv = CallTo16(cs_reg << 16 | ip_reg, ds_reg);
90 dprintf_exec(stddeb,"rv = %x\n", rv);
96 void StartNewTask(HINSTANCE hInst)
98 struct w_files * wpnt;
99 struct w_files * w;
100 int cs_reg, ds_reg, ss_reg, ip_reg, sp_reg;
101 int rv;
102 int segment;
104 dprintf_exec(stddeb,
105 "StartNewTask() before searching hInst=%04X !\n", hInst);
106 wpnt = wine_files;
107 while (wpnt && wpnt->hinstance != hInst) wpnt = wpnt->next;
108 if (wpnt == NULL) return;
109 dprintf_exec(stddeb,"StartNewTask() // before FixupSegment !\n");
110 for(w = wpnt; w; w = w->next) {
111 for (segment = 0; segment < w->ne->ne_header->n_segment_tab; segment++) {
112 if (NE_FixupSegment(w, segment) < 0) {
113 myerror("fixup failed.");
117 dprintf_exec(stddeb,"StartNewTask() before InitializeLoadedNewDLLs !\n");
118 InitializeLoadedNewDLLs(hInst);
119 dprintf_exec(stddeb,"StartNewTask() before setup register !\n");
120 ds_reg = (wpnt->ne->selector_table[wpnt->ne->ne_header->auto_data_seg-1].selector);
121 cs_reg = wpnt->ne->selector_table[wpnt->ne->ne_header->cs-1].selector;
122 ip_reg = wpnt->ne->ne_header->ip;
123 ss_reg = wpnt->ne->selector_table[wpnt->ne->ne_header->ss-1].selector;
124 sp_reg = wpnt->ne->ne_header->sp;
126 dprintf_exec(stddeb,"StartNewTask() before CallToInit16() !\n");
127 rv = CallToInit16(cs_reg << 16 | ip_reg, ss_reg << 16 | sp_reg, ds_reg);
128 dprintf_exec(stddeb,"rv = %x\n", rv);
132 #else
133 void StartNewTask (HINSTANCE hInst)
135 fprintf(stdnimp, "StartNewTask(): Not yet implemented\n");
137 #endif
139 /**********************************************************************
140 * LoadModule [KERNEL.45]
142 HANDLE LoadModule(LPSTR modulefile, LPVOID lpParamBlk)
144 PARAMBLOCK *pblk = lpParamBlk;
145 WORD *lpCmdShow;
146 dprintf_exec(stddeb,"LoadModule '%s' %p\n", modulefile, lpParamBlk);
147 if (lpParamBlk == NULL) return 0;
148 lpCmdShow = (WORD *)pblk->lpCmdShow;
149 return WinExec(pblk->lpCmdLine, lpCmdShow[1]);
153 /**********************************************************************
154 * WinExec [KERNEL.166]
156 WORD WinExec(LPSTR lpCmdLine, WORD nCmdShow)
158 int c = 0;
159 int x, x2;
160 char *ArgV[20];
161 HINSTANCE hInst = 0;
162 HANDLE hTask = 0;
163 dprintf_exec(stddeb,"WinExec('%s', %04X)\n", lpCmdLine, nCmdShow);
164 /* ArgV[0] = "wine";
165 c = 1; */
166 for (x = x2 = 0; x < strlen(lpCmdLine) + 1; x++) {
167 if ((lpCmdLine[x] == ' ') || (lpCmdLine[x] == '\0')) {
168 ArgV[c] = (char *)malloc(x - x2 + 1);
169 strncpy(ArgV[c], &lpCmdLine[x2], x - x2);
170 ArgV[c][x - x2] = '\0';
171 c++; x2 = x + 1;
174 ArgV[c] = NULL;
175 for (c = 0; ArgV[c] != NULL; c++)
176 dprintf_exec(stddeb,"--> '%s' \n", ArgV[c]);
177 switch(fork()) {
178 case -1:
179 fprintf(stderr,"Can't 'fork' process !\n");
180 break;
181 case 0:
182 if ((hInst = LoadImage(ArgV[0], EXE, 1)) == (HINSTANCE) NULL ) {
183 fprintf(stderr, "wine: can't find %s!.\n", ArgV[0]);
184 fprintf(stderr,"Child process died !\n");
185 exit(1);
187 hTask = CreateNewTask(hInst);
188 dprintf_exec(stddeb,
189 "WinExec // hTask=%04X hInst=%04X !\n", hTask, hInst);
190 StartNewTask(hInst);
192 lpfnMain = (LPFNWINMAIN)GetProcAddress(hInst, (LPSTR)0L);
193 dprintf_exec(stddeb,
194 "WineExec() // lpfnMain=%08X\n", (LONG)lpfnMain);
195 if (lpfnMain != NULL) {
196 (lpfnMain)(hInst, 0, lpCmdLine, nCmdShow);
197 dprintf_exec(stddeb,
198 "WineExec() // after lpfnMain\n");
201 /* hTask = CreateNewTask(0);
202 dprintf_exec(stddeb,
203 "WinExec // New Task hTask=%04X !\n", hTask);
204 execvp(ArgV[0], ArgV); */
206 fprintf(stderr,"Child process died !\n");
207 exit(1);
208 default:
209 dprintf_exec(stddeb,
210 "WinExec (Main process stay alive) hTask=%04X !\n",
211 hTask);
212 break;
214 for (c = 0; ArgV[c] != NULL; c++) free(ArgV[c]);
215 return hTask;
219 /**********************************************************************
220 * ExitWindows [USER.7]
222 BOOL ExitWindows(DWORD dwReserved, WORD wRetCode)
224 dprintf_exec(stdnimp,"EMPTY STUB !!! ExitWindows(%08lX, %04X) !\n",
225 dwReserved, wRetCode);
227 exit(wRetCode);
231 /**********************************************************************
232 * WinHelp [USER.171]
234 BOOL WinHelp(HWND hWnd, LPSTR lpHelpFile, WORD wCommand, DWORD dwData)
236 char str[256];
237 dprintf_exec(stddeb,"WinHelp(%s, %u, %lu)\n",
238 lpHelpFile, wCommand, dwData);
239 switch(wCommand) {
240 case 0:
241 case HELP_HELPONHELP:
242 GetWindowsDirectory(str, sizeof(str));
243 strcat(str, "\\winhelp.exe");
244 dprintf_exec(stddeb,"'%s'\n", str);
245 break;
246 case HELP_INDEX:
247 GetWindowsDirectory(str, sizeof(str));
248 strcat(str, "\\winhelp.exe");
249 dprintf_exec(stddeb,"'%s'\n", str);
250 break;
251 default:
252 return FALSE;
254 WinExec(str, SW_SHOWNORMAL);
255 return(TRUE);