Release 941017
[wine/gsoc-2012-control.git] / misc / exec.c
blob31d3570c863745150b27f1684167634963c68db6
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 "wine.h"
15 #include "windows.h"
16 #include "stddebug.h"
17 /* #define DEBUG_EXEC /* */
18 /* #undef DEBUG_EXEC /* */
19 #include "debug.h"
21 #define HELP_CONTEXT 0x0001
22 #define HELP_QUIT 0x0002
23 #define HELP_INDEX 0x0003
24 #define HELP_CONTENTS 0x0003
25 #define HELP_HELPONHELP 0x0004
26 #define HELP_SETINDEX 0x0005
27 #define HELP_SETCONTENTS 0x0005
28 #define HELP_CONTEXTPOPUP 0x0008
29 #define HELP_FORCEFILE 0x0009
30 #define HELP_KEY 0x0101
31 #define HELP_COMMAND 0x0102
32 #define HELP_PARTIALKEY 0x0105
33 #define HELP_MULTIKEY 0x0201
34 #define HELP_SETWINPOS 0x0203
36 extern struct w_files * wine_files;
38 typedef struct {
39 WORD wEnvSeg;
40 LPSTR lpCmdLine;
41 LPVOID lpCmdShow;
42 DWORD dwReserved;
43 } PARAMBLOCK;
45 typedef BOOL (CALLBACK * LPFNWINMAIN)(HANDLE, HANDLE, LPSTR, int);
48 extern int CallToInit16(unsigned long csip, unsigned long sssp,
49 unsigned short ds);
50 HANDLE CreateNewTask(HINSTANCE hInst);
52 #ifndef WINELIB
53 void InitializeLoadedNewDLLs(HINSTANCE hInst)
55 struct w_files * w;
56 struct w_files * wpnt;
57 int cs_reg, ds_reg, ip_reg;
58 int rv;
60 dprintf_exec(stddeb, "Initializing New DLLs\n");
63 * Initialize libraries
65 dprintf_exec(stddeb,
66 "InitializeLoadedNewDLLs() before searching hInst=%04X !\n", hInst);
67 w = wine_files;
68 while (w && w->hinstance != hInst) w = w->next;
69 if (w == NULL) return;
70 dprintf_exec(stddeb,"InitializeLoadedNewDLLs() // before InitLoop !\n");
71 for(wpnt = w; wpnt; wpnt = wpnt->next)
73 /*
74 * Is this a library?
76 if (wpnt->ne->ne_header->format_flags & 0x8000)
78 if (!(wpnt->ne->ne_header->format_flags & 0x0001))
80 /* Not SINGLEDATA */
81 fprintf(stderr, "Library is not marked SINGLEDATA\n");
82 exit(1);
85 ds_reg = wpnt->ne->selector_table[wpnt->ne->
86 ne_header->auto_data_seg-1].selector;
87 cs_reg = wpnt->ne->selector_table[wpnt->ne->ne_header->cs-1].selector;
88 ip_reg = wpnt->ne->ne_header->ip;
90 dprintf_exec(stddeb, "Initializing %s, cs:ip %04x:%04x, ds %04x\n",
91 wpnt->name, cs_reg, ip_reg, ds_reg);
93 rv = CallTo16(cs_reg << 16 | ip_reg, ds_reg);
94 dprintf_exec(stddeb,"rv = %x\n", rv);
100 void StartNewTask(HINSTANCE hInst)
102 struct w_files * wpnt;
103 struct w_files * w;
104 int cs_reg, ds_reg, ss_reg, ip_reg, sp_reg;
105 int rv;
106 int segment;
108 dprintf_exec(stddeb,
109 "StartNewTask() before searching hInst=%04X !\n", hInst);
110 wpnt = wine_files;
111 while (wpnt && wpnt->hinstance != hInst) wpnt = wpnt->next;
112 if (wpnt == NULL) return;
113 dprintf_exec(stddeb,"StartNewTask() // before FixupSegment !\n");
114 for(w = wpnt; w; w = w->next) {
115 for (segment = 0; segment < w->ne->ne_header->n_segment_tab; segment++) {
116 if (FixupSegment(w, segment) < 0) {
117 myerror("fixup failed.");
121 dprintf_exec(stddeb,"StartNewTask() before InitializeLoadedNewDLLs !\n");
122 InitializeLoadedNewDLLs(hInst);
123 dprintf_exec(stddeb,"StartNewTask() before setup register !\n");
124 ds_reg = (wpnt->ne->selector_table[wpnt->ne->ne_header->auto_data_seg-1].selector);
125 cs_reg = wpnt->ne->selector_table[wpnt->ne->ne_header->cs-1].selector;
126 ip_reg = wpnt->ne->ne_header->ip;
127 ss_reg = wpnt->ne->selector_table[wpnt->ne->ne_header->ss-1].selector;
128 sp_reg = wpnt->ne->ne_header->sp;
130 dprintf_exec(stddeb,"StartNewTask() before CallToInit16() !\n");
131 rv = CallToInit16(cs_reg << 16 | ip_reg, ss_reg << 16 | sp_reg, ds_reg);
132 printf ("rv = %x\n", rv);
136 #else
137 void StartNewTask (HINSTANCE hInst)
139 printf ("Not yet implemented\n");
141 #endif
143 /**********************************************************************
144 * LoadModule [KERNEL.45]
146 HANDLE LoadModule(LPSTR modulefile, LPVOID lpParamBlk)
148 PARAMBLOCK *pblk = lpParamBlk;
149 WORD *lpCmdShow;
150 dprintf_exec(stddeb,"LoadModule '%s' %08X\n", modulefile, lpParamBlk);
151 if (lpParamBlk == NULL) return 0;
152 lpCmdShow = (WORD *)pblk->lpCmdShow;
153 return WinExec(pblk->lpCmdLine, lpCmdShow[1]);
157 /**********************************************************************
158 * WinExec [KERNEL.166]
160 WORD WinExec(LPSTR lpCmdLine, WORD nCmdShow)
162 int c = 0;
163 int x, x2;
164 char *ArgV[20];
165 LPFNWINMAIN lpfnMain;
166 HINSTANCE hInst = 0;
167 HANDLE hTask = 0;
168 dprintf_exec(stddeb,"WinExec('%s', %04X)\n", lpCmdLine, nCmdShow);
169 /* ArgV[0] = "wine";
170 c = 1; */
171 for (x = x2 = 0; x < strlen(lpCmdLine) + 1; x++) {
172 if ((lpCmdLine[x] == ' ') || (lpCmdLine[x] == '\0')) {
173 ArgV[c] = (char *)malloc(x - x2 + 1);
174 strncpy(ArgV[c], &lpCmdLine[x2], x - x2);
175 ArgV[c][x - x2] = '\0';
176 c++; x2 = x + 1;
179 ArgV[c] = NULL;
180 for (c = 0; ArgV[c] != NULL; c++)
181 dprintf_exec(stddeb,"--> '%s' \n", ArgV[c]);
182 switch(fork()) {
183 case -1:
184 fprintf(stderr,"Can't 'fork' process !\n");
185 break;
186 case 0:
187 if ((hInst = LoadImage(ArgV[0], EXE, 1)) == (HINSTANCE) NULL ) {
188 fprintf(stderr, "wine: can't find %s!.\n", ArgV[0]);
189 fprintf(stderr,"Child process died !\n");
190 exit(1);
192 hTask = CreateNewTask(hInst);
193 dprintf_exec(stddeb,
194 "WinExec // hTask=%04X hInst=%04X !\n", hTask, hInst);
195 StartNewTask(hInst);
197 lpfnMain = (LPFNWINMAIN)GetProcAddress(hInst, (LPSTR)0L);
198 dprintf_exec(stddeb,
199 "WineExec() // lpfnMain=%08X\n", (LONG)lpfnMain);
200 if (lpfnMain != NULL) {
201 (lpfnMain)(hInst, 0, lpCmdLine, nCmdShow);
202 dprintf_exec(stddeb,
203 "WineExec() // after lpfnMain\n");
206 /* hTask = CreateNewTask(0);
207 dprintf_exec(stddeb,
208 "WinExec // New Task hTask=%04X !\n", hTask);
209 execvp(ArgV[0], ArgV); */
211 fprintf(stderr,"Child process died !\n");
212 exit(1);
213 default:
214 dprintf_exec(stddeb,
215 "WinExec (Main process stay alive) hTask=%04X !\n",
216 hTask);
217 break;
219 for (c = 0; ArgV[c] != NULL; c++) free(ArgV[c]);
220 return hTask;
224 /**********************************************************************
225 * ExitWindows [USER.7]
227 BOOL ExitWindows(DWORD dwReserved, WORD wRetCode)
229 dprintf_exec(stdnimp,"EMPTY STUB !!! ExitWindows(%08X, %04X) !\n",
230 dwReserved, wRetCode);
234 /**********************************************************************
235 * WinHelp [USER.171]
237 BOOL WinHelp(HWND hWnd, LPSTR lpHelpFile, WORD wCommand, DWORD dwData)
239 char str[256];
240 dprintf_exec(stddeb,"WinHelp(%s, %u, %lu)\n",
241 lpHelpFile, wCommand, dwData);
242 switch(wCommand) {
243 case 0:
244 case HELP_HELPONHELP:
245 GetWindowsDirectory(str, sizeof(str));
246 strcat(str, "\\winhelp.exe");
247 dprintf_exec(stddeb,"'%s'\n", str);
248 break;
249 case HELP_INDEX:
250 GetWindowsDirectory(str, sizeof(str));
251 strcat(str, "\\winhelp.exe");
252 dprintf_exec(stddeb,"'%s'\n", str);
253 break;
254 default:
255 return FALSE;
257 WinExec(str, SW_SHOWNORMAL);
258 return(TRUE);