ntdll: Check and fix PE header protections.
[wine.git] / programs / wineconsole / wineconsole.c
blob52edfb3aa155d33438403380ca664cd57264260a
1 /*
2 * an application for displaying Win32 console
4 * Copyright 2001, 2002 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include "wine/server.h"
27 #include "winecon_private.h"
28 #include "winnls.h"
29 #include "winuser.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
35 void WINECON_Fatal(const char* msg)
37 WINE_ERR("%s\n", msg);
38 ExitProcess(0);
41 static void printf_res(UINT uResId, ...)
43 WCHAR buffer[1024];
44 CHAR ansi[1024];
45 va_list args;
47 va_start(args, uResId);
48 LoadStringW(GetModuleHandleW(NULL), uResId, buffer, sizeof(buffer)/sizeof(buffer[0]));
49 WideCharToMultiByte(CP_UNIXCP, 0, buffer, -1, ansi, sizeof(ansi), NULL, NULL);
50 vprintf(ansi, args);
51 va_end(args);
54 static void WINECON_Usage(void)
56 printf_res(IDS_USAGE_HEADER);
57 printf_res(IDS_USAGE_BACKEND);
58 printf_res(IDS_USAGE_COMMAND);
59 printf_res(IDS_USAGE_FOOTER);
62 /******************************************************************
63 * WINECON_FetchCells
65 * updates the local copy of cells (band to update)
67 static void WINECON_FetchCells(struct inner_data* data, int upd_tp, int upd_bm)
69 SERVER_START_REQ( read_console_output )
71 req->handle = wine_server_obj_handle( data->hConOut );
72 req->x = 0;
73 req->y = upd_tp;
74 req->mode = CHAR_INFO_MODE_TEXTATTR;
75 req->wrap = TRUE;
76 wine_server_set_reply( req, &data->cells[upd_tp * data->curcfg.sb_width],
77 (upd_bm-upd_tp+1) * data->curcfg.sb_width * sizeof(CHAR_INFO) );
78 wine_server_call( req );
80 SERVER_END_REQ;
81 data->fnRefresh(data, upd_tp, upd_bm);
84 /******************************************************************
85 * WINECON_NotifyWindowChange
87 * Inform server that visible window on sb has changed
89 void WINECON_NotifyWindowChange(struct inner_data* data)
91 SERVER_START_REQ( set_console_output_info )
93 req->handle = wine_server_obj_handle( data->hConOut );
94 req->win_left = data->curcfg.win_pos.X;
95 req->win_top = data->curcfg.win_pos.Y;
96 req->win_right = data->curcfg.win_pos.X + data->curcfg.win_width - 1;
97 req->win_bottom = data->curcfg.win_pos.Y + data->curcfg.win_height - 1;
98 req->mask = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
99 wine_server_call( req );
101 SERVER_END_REQ;
104 /******************************************************************
105 * WINECON_SetHistorySize
109 static BOOL WINECON_SetHistorySize(HANDLE hConIn, int size)
111 BOOL ret;
113 SERVER_START_REQ(set_console_input_info)
115 req->handle = wine_server_obj_handle( hConIn );
116 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_SIZE;
117 req->history_size = size;
118 ret = !wine_server_call_err( req );
120 SERVER_END_REQ;
121 return ret;
124 /******************************************************************
125 * WINECON_SetHistoryMode
129 static BOOL WINECON_SetHistoryMode(HANDLE hConIn, int mode)
131 BOOL ret;
133 SERVER_START_REQ(set_console_input_info)
135 req->handle = wine_server_obj_handle( hConIn );
136 req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_MODE;
137 req->history_mode = mode;
138 ret = !wine_server_call_err( req );
140 SERVER_END_REQ;
141 return ret;
144 /******************************************************************
145 * WINECON_GetConsoleTitle
149 BOOL WINECON_GetConsoleTitle(HANDLE hConIn, WCHAR* buffer, size_t len)
151 BOOL ret;
153 if (len < sizeof(WCHAR)) return FALSE;
155 SERVER_START_REQ( get_console_input_info )
157 req->handle = wine_server_obj_handle( hConIn );
158 wine_server_set_reply( req, buffer, len - sizeof(WCHAR) );
159 if ((ret = !wine_server_call_err( req )))
161 len = wine_server_reply_size( reply );
162 buffer[len / sizeof(WCHAR)] = 0;
165 SERVER_END_REQ;
166 return ret;
169 /******************************************************************
170 * WINECON_SetEditionMode
174 static BOOL WINECON_SetEditionMode(HANDLE hConIn, int edition_mode)
176 BOOL ret;
178 SERVER_START_REQ( set_console_input_info )
180 req->handle = wine_server_obj_handle( hConIn );
181 req->mask = SET_CONSOLE_INPUT_INFO_EDITION_MODE;
182 req->edition_mode = edition_mode;
183 ret = !wine_server_call_err( req );
185 SERVER_END_REQ;
186 return ret;
189 /******************************************************************
190 * WINECON_GrabChanges
192 * A change occurs, try to figure out which
194 int WINECON_GrabChanges(struct inner_data* data)
196 struct console_renderer_event evts[256];
197 int i, num, ev_found;
198 HANDLE h;
200 SERVER_START_REQ( get_console_renderer_events )
202 wine_server_set_reply( req, evts, sizeof(evts) );
203 req->handle = wine_server_obj_handle( data->hSynchro );
204 if (!wine_server_call_err( req )) num = wine_server_reply_size(reply) / sizeof(evts[0]);
205 else num = 0;
207 SERVER_END_REQ;
208 if (!num) {WINE_WARN("hmm renderer signaled but no events available\n"); return 1;}
210 /* FIXME: should do some event compression here (cursor pos, update) */
211 /* step 1: keep only last cursor pos event */
212 ev_found = -1;
213 for (i = num - 1; i >= 0; i--)
215 if (evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT)
217 if (ev_found != -1)
218 evts[i].event = CONSOLE_RENDERER_NONE_EVENT;
219 ev_found = i;
222 /* step 2: manage update events */
223 ev_found = -1;
224 for (i = 0; i < num; i++)
226 if (evts[i].event == CONSOLE_RENDERER_NONE_EVENT ||
227 evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT ||
228 evts[i].event == CONSOLE_RENDERER_CURSOR_GEOM_EVENT) continue;
229 if (evts[i].event != CONSOLE_RENDERER_UPDATE_EVENT)
231 ev_found = -1;
232 continue;
235 if (ev_found != -1 && /* Only 2 cases where they CANNOT merge */
236 !(evts[i ].u.update.bottom + 1 < evts[ev_found].u.update.top ||
237 evts[ev_found].u.update.bottom + 1 < evts[i ].u.update.top))
239 evts[i].u.update.top = min(evts[i ].u.update.top,
240 evts[ev_found].u.update.top);
241 evts[i].u.update.bottom = max(evts[i ].u.update.bottom,
242 evts[ev_found].u.update.bottom);
243 evts[ev_found].event = CONSOLE_RENDERER_NONE_EVENT;
245 ev_found = i;
248 WINE_TRACE("Events:");
249 for (i = 0; i < num; i++)
251 switch (evts[i].event)
253 case CONSOLE_RENDERER_NONE_EVENT:
254 WINE_TRACE(" NOP");
255 break;
256 case CONSOLE_RENDERER_TITLE_EVENT:
257 WINE_TRACE(" title()");
258 data->fnSetTitle(data);
259 break;
260 case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
261 SERVER_START_REQ( open_console )
263 req->from = wine_server_obj_handle( data->hConIn );
264 req->access = GENERIC_READ | GENERIC_WRITE;
265 req->attributes = 0;
266 req->share = FILE_SHARE_READ | FILE_SHARE_WRITE;
267 h = wine_server_call_err( req ) ? 0 : wine_server_ptr_handle(reply->handle);
269 SERVER_END_REQ;
270 WINE_TRACE(" active(%p)", h);
271 if (h)
273 CloseHandle(data->hConOut);
274 data->hConOut = h;
276 break;
277 case CONSOLE_RENDERER_SB_RESIZE_EVENT:
278 if (data->curcfg.sb_width != evts[i].u.resize.width ||
279 data->curcfg.sb_height != evts[i].u.resize.height)
281 WINE_TRACE(" resize(%d,%d)", evts[i].u.resize.width, evts[i].u.resize.height);
282 data->curcfg.sb_width = evts[i].u.resize.width;
283 data->curcfg.sb_height = evts[i].u.resize.height;
285 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
286 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
288 if (!data->cells) WINECON_Fatal("OOM\n");
289 data->fnResizeScreenBuffer(data);
290 data->fnComputePositions(data);
292 break;
293 case CONSOLE_RENDERER_UPDATE_EVENT:
294 WINE_TRACE(" update(%d,%d)", evts[i].u.update.top, evts[i].u.update.bottom);
295 WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
296 break;
297 case CONSOLE_RENDERER_CURSOR_POS_EVENT:
298 if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
300 WINE_TRACE(" curs-pos(%d,%d)",evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
301 data->cursor.X = evts[i].u.cursor_pos.x;
302 data->cursor.Y = evts[i].u.cursor_pos.y;
303 data->fnPosCursor(data);
305 break;
306 case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
307 if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size ||
308 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
310 WINE_TRACE(" curs-geom(%d,%d)",
311 evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
312 data->fnShapeCursor(data, evts[i].u.cursor_geom.size,
313 evts[i].u.cursor_geom.visible, FALSE);
315 break;
316 case CONSOLE_RENDERER_DISPLAY_EVENT:
317 if (evts[i].u.display.left != data->curcfg.win_pos.X)
319 WINE_TRACE(" h-scroll(%d)", evts[i].u.display.left);
320 data->fnScroll(data, evts[i].u.display.left, TRUE);
321 data->fnPosCursor(data);
323 if (evts[i].u.display.top != data->curcfg.win_pos.Y)
325 WINE_TRACE(" v-scroll(%d)", evts[i].u.display.top);
326 data->fnScroll(data, evts[i].u.display.top, FALSE);
327 data->fnPosCursor(data);
329 if (evts[i].u.display.width != data->curcfg.win_width ||
330 evts[i].u.display.height != data->curcfg.win_height)
332 WINE_TRACE(" win-size(%d,%d)", evts[i].u.display.width, evts[i].u.display.height);
333 data->curcfg.win_width = evts[i].u.display.width;
334 data->curcfg.win_height = evts[i].u.display.height;
335 data->fnComputePositions(data);
337 break;
338 case CONSOLE_RENDERER_EXIT_EVENT:
339 WINE_TRACE(". Exit!!\n");
340 return 0;
341 default:
342 WINE_FIXME("Unknown event type (%d)\n", evts[i].event);
346 WINE_TRACE(".\n");
347 return 1;
350 /******************************************************************
351 * WINECON_SetConfig
353 * Apply to data all the configuration elements from cfg. This includes modification
354 * of server side equivalent and visual parts.
355 * If force is FALSE, only the changed items are modified.
357 void WINECON_SetConfig(struct inner_data* data, const struct config_data* cfg)
359 if (data->curcfg.cursor_size != cfg->cursor_size ||
360 data->curcfg.cursor_visible != cfg->cursor_visible)
362 CONSOLE_CURSOR_INFO cinfo;
363 cinfo.dwSize = cfg->cursor_size;
364 /* <FIXME>: this hack is needed to pass thru the invariant test operation on server side
365 * (no notification is sent when invariant operation is requested
367 cinfo.bVisible = !cfg->cursor_visible;
368 SetConsoleCursorInfo(data->hConOut, &cinfo);
369 /* </FIXME> */
370 cinfo.bVisible = cfg->cursor_visible;
371 /* this shall update (through notif) curcfg */
372 SetConsoleCursorInfo(data->hConOut, &cinfo);
374 if (data->curcfg.history_size != cfg->history_size)
376 data->curcfg.history_size = cfg->history_size;
377 WINECON_SetHistorySize(data->hConIn, cfg->history_size);
379 if (data->curcfg.history_nodup != cfg->history_nodup)
381 data->curcfg.history_nodup = cfg->history_nodup;
382 WINECON_SetHistoryMode(data->hConIn, cfg->history_nodup);
384 data->curcfg.menu_mask = cfg->menu_mask;
385 data->curcfg.quick_edit = cfg->quick_edit;
386 if (1 /* FIXME: font info has changed */)
388 data->fnSetFont(data, cfg->face_name, cfg->cell_height, cfg->font_weight);
390 if (data->curcfg.def_attr != cfg->def_attr)
392 data->curcfg.def_attr = cfg->def_attr;
393 SetConsoleTextAttribute(data->hConOut, cfg->def_attr);
395 /* now let's look at the window / sb size changes...
396 * since the server checks that sb is always bigger than window,
397 * we have to take care of doing the operations in the right order
399 /* a set of macros to make things easier to read
400 * The Test<A><B> macros test if the <A> (width/height) needs to be changed
401 * for <B> (window / ScreenBuffer)
402 * The Change<A><B> actually modify the <B> dimension of <A>.
404 #define TstSBfWidth() (data->curcfg.sb_width != cfg->sb_width)
405 #define TstWinWidth() (data->curcfg.win_width != cfg->win_width)
407 #define ChgSBfWidth() do {c.X = cfg->sb_width; \
408 c.Y = data->curcfg.sb_height;\
409 SetConsoleScreenBufferSize(data->hConOut, c);\
410 } while (0)
411 #define ChgWinWidth() do {pos.Left = pos.Top = 0; \
412 pos.Right = cfg->win_width - data->curcfg.win_width; \
413 pos.Bottom = 0; \
414 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
415 } while (0)
416 #define TstSBfHeight() (data->curcfg.sb_height != cfg->sb_height)
417 #define TstWinHeight() (data->curcfg.win_height != cfg->win_height)
419 /* since we're going to apply height after width is done, we use width as defined
420 * in cfg, and not in data->curcfg because if won't be updated yet */
421 #define ChgSBfHeight() do {c.X = cfg->sb_width; c.Y = cfg->sb_height; \
422 SetConsoleScreenBufferSize(data->hConOut, c); \
423 } while (0)
424 #define ChgWinHeight() do {pos.Left = pos.Top = 0; \
425 pos.Right = 0; \
426 pos.Bottom = cfg->win_height - data->curcfg.win_height; \
427 SetConsoleWindowInfo(data->hConOut, FALSE, &pos);\
428 } while (0)
432 COORD c;
433 SMALL_RECT pos;
435 if (TstSBfWidth())
437 if (TstWinWidth())
439 /* we're changing both at the same time, do it in the right order */
440 if (cfg->sb_width >= data->curcfg.win_width)
442 ChgSBfWidth(); ChgWinWidth();
444 else
446 ChgWinWidth(); ChgSBfWidth();
449 else ChgSBfWidth();
451 else if (TstWinWidth()) ChgWinWidth();
452 if (TstSBfHeight())
454 if (TstWinHeight())
456 if (cfg->sb_height >= data->curcfg.win_height)
458 ChgSBfHeight(); ChgWinHeight();
460 else
462 ChgWinHeight(); ChgSBfHeight();
465 else ChgSBfHeight();
467 else if (TstWinHeight()) ChgWinHeight();
468 } while (0);
469 #undef TstSBfWidth
470 #undef TstWinWidth
471 #undef ChgSBfWidth
472 #undef ChgWinWidth
473 #undef TstSBfHeight
474 #undef TstWinHeight
475 #undef ChgSBfHeight
476 #undef ChgWinHeight
478 data->curcfg.exit_on_die = cfg->exit_on_die;
479 if (data->curcfg.edition_mode != cfg->edition_mode)
481 data->curcfg.edition_mode = cfg->edition_mode;
482 WINECON_SetEditionMode(data->hConIn, cfg->edition_mode);
484 /* we now need to gather all events we got from the operations above,
485 * in order to get data correctly updated
487 WINECON_GrabChanges(data);
490 /******************************************************************
491 * WINECON_Delete
493 * Destroy wineconsole internal data
495 static void WINECON_Delete(struct inner_data* data)
497 if (!data) return;
499 if (data->fnDeleteBackend) data->fnDeleteBackend(data);
500 if (data->hConIn) CloseHandle(data->hConIn);
501 if (data->hConOut) CloseHandle(data->hConOut);
502 if (data->hSynchro) CloseHandle(data->hSynchro);
503 HeapFree(GetProcessHeap(), 0, data->cells);
504 HeapFree(GetProcessHeap(), 0, data);
507 /******************************************************************
508 * WINECON_GetServerConfig
510 * Fills data->curcfg with the actual configuration running in the server
511 * (getting real information on the server, and not relying on cached
512 * information in data)
514 static BOOL WINECON_GetServerConfig(struct inner_data* data)
516 BOOL ret;
518 SERVER_START_REQ(get_console_input_info)
520 req->handle = wine_server_obj_handle( data->hConIn );
521 ret = !wine_server_call_err( req );
522 data->curcfg.history_size = reply->history_size;
523 data->curcfg.history_nodup = reply->history_mode;
524 data->curcfg.edition_mode = reply->edition_mode;
526 SERVER_END_REQ;
527 if (!ret) return FALSE;
528 SERVER_START_REQ(get_console_output_info)
530 req->handle = wine_server_obj_handle( data->hConOut );
531 ret = !wine_server_call_err( req );
532 data->curcfg.cursor_size = reply->cursor_size;
533 data->curcfg.cursor_visible = reply->cursor_visible;
534 data->curcfg.def_attr = reply->attr;
535 data->curcfg.sb_width = reply->width;
536 data->curcfg.sb_height = reply->height;
537 data->curcfg.win_width = reply->win_right - reply->win_left + 1;
538 data->curcfg.win_height = reply->win_bottom - reply->win_top + 1;
540 SERVER_END_REQ;
541 WINECON_DumpConfig("first cfg: ", &data->curcfg);
543 return ret;
546 /******************************************************************
547 * WINECON_Init
549 * Initialisation part I. Creation of server object (console input and
550 * active screen buffer)
552 static struct inner_data* WINECON_Init(HINSTANCE hInst, DWORD pid, LPCWSTR appname,
553 enum init_return (*backend)(struct inner_data*),
554 INT nCmdShow)
556 struct inner_data* data = NULL;
557 DWORD ret;
558 struct config_data cfg;
559 STARTUPINFOW si;
561 data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
562 if (!data) return 0;
564 GetStartupInfoW(&si);
566 if (pid == 0)
568 if (!si.lpTitle) WINECON_Fatal("Should have a title set");
569 appname = si.lpTitle;
572 data->nCmdShow = nCmdShow;
573 /* load settings */
574 WINECON_RegLoad(appname, &cfg);
576 /* some overrides */
577 if (pid == 0)
579 if (si.dwFlags & STARTF_USECOUNTCHARS)
581 cfg.sb_width = si.dwXCountChars;
582 cfg.sb_height = si.dwYCountChars;
584 if (si.dwFlags & STARTF_USEFILLATTRIBUTE)
585 cfg.def_attr = si.dwFillAttribute;
586 /* should always be defined */
589 /* the handles here are created without the whistles and bells required by console
590 * (mainly because wineconsole doesn't need it)
591 * - they are not inheritable
592 * - hConIn is not synchronizable
594 SERVER_START_REQ(alloc_console)
596 req->access = GENERIC_READ | GENERIC_WRITE;
597 req->attributes = 0;
598 req->pid = pid;
599 req->input_fd = -1;
601 ret = !wine_server_call_err( req );
602 data->hConIn = wine_server_ptr_handle( reply->handle_in );
603 data->hSynchro = wine_server_ptr_handle( reply->event );
605 SERVER_END_REQ;
606 if (!ret) goto error;
607 WINE_TRACE("using hConIn %p, hSynchro event %p\n", data->hConIn, data->hSynchro);
609 SERVER_START_REQ(create_console_output)
611 req->handle_in = wine_server_obj_handle( data->hConIn );
612 req->access = GENERIC_WRITE|GENERIC_READ;
613 req->attributes = 0;
614 req->share = FILE_SHARE_READ|FILE_SHARE_WRITE;
615 req->fd = -1;
616 ret = !wine_server_call_err( req );
617 data->hConOut = wine_server_ptr_handle( reply->handle_out );
619 SERVER_END_REQ;
620 if (!ret) goto error;
621 WINE_TRACE("using hConOut %p\n", data->hConOut);
623 /* filling data->curcfg from cfg */
624 switch ((*backend)(data))
626 case init_not_supported:
627 if (backend == WCCURSES_InitBackend)
629 if (WCUSER_InitBackend( data ) != init_success) break;
631 else if (backend == WCUSER_InitBackend)
633 if (WCCURSES_InitBackend( data ) != init_success) break;
635 /* fall through */
636 case init_success:
637 WINECON_GetServerConfig(data);
638 data->cells = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
639 data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
640 if (!data->cells) WINECON_Fatal("OOM\n");
641 data->fnResizeScreenBuffer(data);
642 data->fnComputePositions(data);
643 WINECON_SetConfig(data, &cfg);
644 data->curcfg.registry = cfg.registry;
645 WINECON_DumpConfig("fint", &data->curcfg);
646 SERVER_START_REQ( set_console_input_info )
648 req->handle = wine_server_obj_handle( data->hConIn );
649 req->win = wine_server_user_handle( data->hWnd );
650 req->mask = SET_CONSOLE_INPUT_INFO_TITLE |
651 SET_CONSOLE_INPUT_INFO_WIN;
652 wine_server_add_data( req, appname, lstrlenW(appname) * sizeof(WCHAR) );
653 ret = !wine_server_call_err( req );
655 SERVER_END_REQ;
656 if (!ret) goto error;
658 return data;
659 case init_failed:
660 break;
663 error:
664 WINE_ERR("failed to init.\n");
666 WINECON_Delete(data);
667 return NULL;
670 /******************************************************************
671 * WINECON_Spawn
673 * Spawn the child process when invoked with wineconsole foo bar
675 static BOOL WINECON_Spawn(struct inner_data* data, LPWSTR cmdLine)
677 PROCESS_INFORMATION info;
678 STARTUPINFOW startup;
679 BOOL done;
681 /* we're in the case wineconsole <exe> <options>... spawn the new process */
682 memset(&startup, 0, sizeof(startup));
683 startup.cb = sizeof(startup);
684 startup.dwFlags = STARTF_USESTDHANDLES;
686 /* the attributes of wineconsole's handles are not adequate for inheritance, so
687 * get them with the correct attributes before process creation
689 if (!DuplicateHandle(GetCurrentProcess(), data->hConIn, GetCurrentProcess(),
690 &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
691 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
692 &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
693 !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
694 &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
696 WINE_ERR("Can't dup handles\n");
697 /* no need to delete handles, we're exiting the program anyway */
698 return FALSE;
701 done = CreateProcessW(NULL, cmdLine, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
703 /* we no longer need the handles passed to the child for the console */
704 CloseHandle(startup.hStdInput);
705 CloseHandle(startup.hStdOutput);
706 CloseHandle(startup.hStdError);
708 CloseHandle(info.hProcess);
709 CloseHandle(info.hThread);
711 return done;
714 struct wc_init {
715 LPCSTR ptr;
716 enum {from_event, from_process_name} mode;
717 enum init_return (*backend)(struct inner_data*);
718 HANDLE event;
721 #define WINECON_CMD_SHOW_USAGE 0x10000
723 /******************************************************************
724 * WINECON_ParseOptions
726 * RETURNS
727 * On success: 0
728 * On error: error string id optionally with the CMD_SHOW_USAGE flag
730 static UINT WINECON_ParseOptions(const char* lpCmdLine, struct wc_init* wci)
732 memset(wci, 0, sizeof(*wci));
733 wci->ptr = lpCmdLine;
734 wci->mode = from_process_name;
735 wci->backend = WCUSER_InitBackend;
737 for (;;)
739 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
740 if (wci->ptr[0] != '-') break;
741 if (strncmp(wci->ptr, "--use-event=", 12) == 0)
743 char* end;
744 wci->event = (HANDLE)strtol(wci->ptr + 12, &end, 10);
745 if (end == wci->ptr + 12) return IDS_CMD_INVALID_EVENT_ID;
746 wci->mode = from_event;
747 wci->ptr = end;
748 wci->backend = WCUSER_InitBackend;
750 else if (strncmp(wci->ptr, "--backend=", 10) == 0)
752 if (strncmp(wci->ptr + 10, "user", 4) == 0)
754 wci->backend = WCUSER_InitBackend;
755 wci->ptr += 14;
757 else if (strncmp(wci->ptr + 10, "curses", 6) == 0)
759 wci->backend = WCCURSES_InitBackend;
760 wci->ptr += 16;
762 else
763 return IDS_CMD_INVALID_BACKEND;
765 else
766 return IDS_CMD_INVALID_OPTION|WINECON_CMD_SHOW_USAGE;
769 if (wci->mode == from_event)
770 return 0;
772 while (*wci->ptr == ' ' || *wci->ptr == '\t') wci->ptr++;
773 if (*wci->ptr == 0)
774 return IDS_CMD_ABOUT|WINECON_CMD_SHOW_USAGE;
776 return 0;
779 /******************************************************************
780 * WinMain
782 * wineconsole can either be started as:
783 * wineconsole --use-event=<int> used when a new console is created (AllocConsole)
784 * wineconsole <pgm> <arguments> used to start the program <pgm> from the command line in
785 * a freshly created console
786 * --backend=(curses|user) can also be used to select the desired backend
788 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nCmdShow)
790 struct inner_data* data;
791 int ret = 1;
792 struct wc_init wci;
794 if ((ret = WINECON_ParseOptions(lpCmdLine, &wci)) != 0)
796 printf_res(ret & 0xffff);
797 if (ret & WINECON_CMD_SHOW_USAGE)
798 WINECON_Usage();
799 return 0;
802 switch (wci.mode)
804 case from_event:
805 /* case of wineconsole <evt>, signal process that created us that we're up and running */
806 if (!(data = WINECON_Init(hInst, 0, NULL, wci.backend, nCmdShow))) return 0;
807 ret = SetEvent(wci.event);
808 if (!ret) WINE_ERR("SetEvent failed.\n");
809 break;
810 case from_process_name:
812 WCHAR buffer[256];
814 MultiByteToWideChar(CP_ACP, 0, wci.ptr, -1, buffer, sizeof(buffer) / sizeof(buffer[0]));
816 if (!(data = WINECON_Init(hInst, GetCurrentProcessId(), buffer, wci.backend, nCmdShow)))
817 return 0;
818 ret = WINECON_Spawn(data, buffer);
819 if (!ret)
821 WINECON_Delete(data);
822 printf_res(IDS_CMD_LAUNCH_FAILED, wine_dbgstr_a(wci.ptr));
823 return 0;
826 break;
827 default:
828 return 0;
831 if (ret)
833 WINE_TRACE("calling MainLoop.\n");
834 ret = data->fnMainLoop(data);
837 WINECON_Delete(data);
839 return ret;