Forbids selecting (for clipboard) areas larger than the actual
[wine] / programs / wineconsole / wineconsole.c
1 /*
2  * an application for displaying Win32 console
3  *
4  * Copyright 2001, 2002 Eric Pouech
5  *
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.
10  *
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.
15  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include "wine/server.h"
26 #include "winecon_private.h"
27 #include "winnls.h"
28
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(wineconsole);
32
33 void WINECON_Fatal(const char* msg)
34 {
35     WINE_ERR("%s\n", msg);
36     ExitProcess(0);
37 }
38
39 /******************************************************************
40  *              WINECON_FetchCells
41  *
42  * updates the local copy of cells (band to update)
43  */
44 void WINECON_FetchCells(struct inner_data* data, int upd_tp, int upd_bm)
45 {
46     SERVER_START_REQ( read_console_output )
47     {
48         req->handle = (obj_handle_t)data->hConOut;
49         req->x      = 0;
50         req->y      = upd_tp;
51         req->mode   = CHAR_INFO_MODE_TEXTATTR;
52         req->wrap   = TRUE;
53         wine_server_set_reply( req, &data->cells[upd_tp * data->curcfg.sb_width],
54                                (upd_bm-upd_tp+1) * data->curcfg.sb_width * sizeof(CHAR_INFO) );
55         wine_server_call( req );
56     }
57     SERVER_END_REQ;
58     data->fnRefresh(data, upd_tp, upd_bm);
59 }
60
61 /******************************************************************
62  *              WINECON_NotifyWindowChange
63  *
64  * Inform server that visible window on sb has changed
65  */
66 void WINECON_NotifyWindowChange(struct inner_data* data)
67 {
68     SERVER_START_REQ( set_console_output_info )
69     {
70         req->handle       = (obj_handle_t)data->hConOut;
71         req->win_left     = data->curcfg.win_pos.X;
72         req->win_top      = data->curcfg.win_pos.Y;
73         req->win_right    = data->curcfg.win_pos.X + data->curcfg.win_width - 1;
74         req->win_bottom   = data->curcfg.win_pos.Y + data->curcfg.win_height - 1;
75         req->mask         = SET_CONSOLE_OUTPUT_INFO_DISPLAY_WINDOW;
76         wine_server_call( req );
77     }
78     SERVER_END_REQ;
79 }
80
81 /******************************************************************
82  *              WINECON_GetHistorySize
83  *
84  *
85  */
86 int     WINECON_GetHistorySize(HANDLE hConIn)
87 {
88     int ret = 0;
89
90     SERVER_START_REQ(get_console_input_info)
91     {
92         req->handle = (obj_handle_t)hConIn;
93         if (!wine_server_call_err( req )) ret = reply->history_size;
94     }
95     SERVER_END_REQ;
96     return ret;
97 }
98
99 /******************************************************************
100  *              WINECON_SetHistorySize
101  *
102  *
103  */
104 BOOL    WINECON_SetHistorySize(HANDLE hConIn, int size)
105 {
106     BOOL        ret;
107
108     SERVER_START_REQ(set_console_input_info)
109     {
110         req->handle = (obj_handle_t)hConIn;
111         req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_SIZE;
112         req->history_size = size;
113         ret = !wine_server_call_err( req );
114     }
115     SERVER_END_REQ;
116     return ret;
117 }
118
119
120 /******************************************************************
121  *              WINECON_GetHistoryMode
122  *
123  *
124  */
125 int     WINECON_GetHistoryMode(HANDLE hConIn)
126 {
127     int ret = 0;
128
129     SERVER_START_REQ(get_console_input_info)
130     {
131         req->handle = (obj_handle_t)hConIn;
132         if (!wine_server_call_err( req )) ret = reply->history_mode;
133     }
134     SERVER_END_REQ;
135     return ret;
136 }
137
138 /******************************************************************
139  *              WINECON_SetHistoryMode
140  *
141  *
142  */
143 BOOL    WINECON_SetHistoryMode(HANDLE hConIn, int mode)
144 {
145     BOOL        ret;
146
147     SERVER_START_REQ(set_console_input_info)
148     {
149         req->handle = (obj_handle_t)hConIn;
150         req->mask = SET_CONSOLE_INPUT_INFO_HISTORY_MODE;
151         req->history_mode = mode;
152         ret = !wine_server_call_err( req );
153     }
154     SERVER_END_REQ;
155     return ret;
156 }
157
158 /******************************************************************
159  *              WINECON_GetConsoleTitle
160  *
161  *
162  */
163 BOOL WINECON_GetConsoleTitle(HANDLE hConIn, WCHAR* buffer, size_t len)
164 {
165     BOOL ret;
166
167     if (len < sizeof(WCHAR)) return FALSE;
168
169     SERVER_START_REQ( get_console_input_info )
170     {
171         req->handle = (obj_handle_t)hConIn;
172         wine_server_set_reply( req, buffer, len - sizeof(WCHAR) );
173         if ((ret = !wine_server_call_err( req )))
174         {
175             len = wine_server_reply_size( reply );
176             buffer[len / sizeof(WCHAR)] = 0;
177         }
178     }
179     SERVER_END_REQ;
180     return ret;
181 }
182
183 /******************************************************************
184  *              WINECON_SetEditionMode
185  *
186  *
187  */
188 static BOOL WINECON_SetEditionMode(HANDLE hConIn, int edition_mode)
189 {
190     BOOL ret;
191
192     SERVER_START_REQ( set_console_input_info )
193     {
194         req->handle = (obj_handle_t)hConIn;
195         req->mask = SET_CONSOLE_INPUT_INFO_EDITION_MODE;
196         req->edition_mode = edition_mode;
197         ret = !wine_server_call_err( req );
198     }
199     SERVER_END_REQ;
200     return ret;
201 }
202
203 /******************************************************************
204  *              WINECON_GrabChanges
205  *
206  * A change occurs, try to figure out which
207  */
208 int     WINECON_GrabChanges(struct inner_data* data)
209 {
210     struct console_renderer_event       evts[256];
211     int i, num, curs = -1;
212     HANDLE h;
213
214     SERVER_START_REQ( get_console_renderer_events )
215     {
216         wine_server_set_reply( req, evts, sizeof(evts) );
217         req->handle = (obj_handle_t)data->hSynchro;
218         if (!wine_server_call_err( req )) num = wine_server_reply_size(reply) / sizeof(evts[0]);
219         else num = 0;
220     }
221     SERVER_END_REQ;
222     if (!num) {WINE_WARN("hmm renderer signaled but no events available\n"); return 1;}
223
224     /* FIXME: should do some event compression here (cursor pos, update) */
225     /* step 1: keep only last cursor pos event */
226     for (i = num - 1; i >= 0; i--)
227     {
228         if (evts[i].event == CONSOLE_RENDERER_CURSOR_POS_EVENT)
229         {
230             if (curs == -1)
231                 curs = i;
232             else
233             {
234                 memmove(&evts[i], &evts[i+1], (num - i - 1) * sizeof(evts[0]));
235                 num--;
236             }
237         }
238     }
239     /* step 2: manage update events */
240     for (i = 0; i < num - 1; i++)
241     {
242         if (evts[i].event == CONSOLE_RENDERER_UPDATE_EVENT &&
243             evts[i+1].event == CONSOLE_RENDERER_UPDATE_EVENT)
244         {
245             /* contiguous */
246             if (evts[i].u.update.bottom + 1 == evts[i+1].u.update.top)
247             {
248                 evts[i].u.update.bottom = evts[i+1].u.update.bottom;
249                 memmove(&evts[i+1], &evts[i+2], (num - i - 2) * sizeof(evts[0]));
250                 num--; i--;
251             }
252             /* already handled cases */
253             else if (evts[i].u.update.top <= evts[i+1].u.update.top &&
254                      evts[i].u.update.bottom >= evts[i+1].u.update.bottom)
255             {
256                 memmove(&evts[i+1], &evts[i+2], (num - i - 2) * sizeof(evts[0]));
257                 num--; i--;
258             }
259         }
260     }
261
262     WINE_TRACE("Events:");
263     for (i = 0; i < num; i++)
264     {
265         switch (evts[i].event)
266         {
267         case CONSOLE_RENDERER_TITLE_EVENT:
268             data->fnSetTitle(data);
269             break;
270         case CONSOLE_RENDERER_ACTIVE_SB_EVENT:
271             SERVER_START_REQ( open_console )
272             {
273                 req->from    = (int)data->hConIn;
274                 req->access  = GENERIC_READ | GENERIC_WRITE;
275                 req->share   = FILE_SHARE_READ | FILE_SHARE_WRITE;
276                 req->inherit = FALSE;
277                 h = wine_server_call_err( req ) ? 0 : (HANDLE)reply->handle;
278             }
279             SERVER_END_REQ;
280             if (WINE_TRACE_ON(wineconsole))
281                 WINE_DPRINTF(" active(%d)", (int)h);
282             if (h)
283             {
284                 CloseHandle(data->hConOut);
285                 data->hConOut = h;
286             }
287             break;
288         case CONSOLE_RENDERER_SB_RESIZE_EVENT:
289             if (data->curcfg.sb_width != evts[i].u.resize.width ||
290                 data->curcfg.sb_height != evts[i].u.resize.height || !data->cells)
291             {
292                 if (WINE_TRACE_ON(wineconsole))
293                     WINE_DPRINTF(" resize(%d,%d)", evts[i].u.resize.width, evts[i].u.resize.height);
294                 data->curcfg.sb_width  = evts[i].u.resize.width;
295                 data->curcfg.sb_height = evts[i].u.resize.height;
296
297                 data->cells = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data->cells,
298                                           data->curcfg.sb_width * data->curcfg.sb_height * sizeof(CHAR_INFO));
299                 if (!data->cells) WINECON_Fatal("OOM\n");
300                 data->fnResizeScreenBuffer(data);
301                 data->fnComputePositions(data);
302             }
303             break;
304         case CONSOLE_RENDERER_UPDATE_EVENT:
305             if (WINE_TRACE_ON(wineconsole))
306                 WINE_DPRINTF(" update(%d,%d)", evts[i].u.update.top, evts[i].u.update.bottom);
307             WINECON_FetchCells(data, evts[i].u.update.top, evts[i].u.update.bottom);
308             break;
309         case CONSOLE_RENDERER_CURSOR_POS_EVENT:
310             if (evts[i].u.cursor_pos.x != data->cursor.X || evts[i].u.cursor_pos.y != data->cursor.Y)
311             {
312                 data->cursor.X = evts[i].u.cursor_pos.x;
313                 data->cursor.Y = evts[i].u.cursor_pos.y;
314                 data->fnPosCursor(data);
315                 if (WINE_TRACE_ON(wineconsole))
316                     WINE_DPRINTF(" curs-pos(%d,%d)",evts[i].u.cursor_pos.x, evts[i].u.cursor_pos.y);
317             }
318             break;
319         case CONSOLE_RENDERER_CURSOR_GEOM_EVENT:
320             if (evts[i].u.cursor_geom.size != data->curcfg.cursor_size ||
321                 evts[i].u.cursor_geom.visible != data->curcfg.cursor_visible)
322             {
323                 data->fnShapeCursor(data, evts[i].u.cursor_geom.size,
324                                     evts[i].u.cursor_geom.visible, FALSE);
325                 if (WINE_TRACE_ON(wineconsole))
326                     WINE_DPRINTF(" curs-geom(%d,%d)",
327                                  evts[i].u.cursor_geom.size, evts[i].u.cursor_geom.visible);
328             }
329             break;
330         case CONSOLE_RENDERER_DISPLAY_EVENT:
331             if (evts[i].u.display.left != data->curcfg.win_pos.X)
332             {
333                 data->fnScroll(data, evts[i].u.display.left, TRUE);
334                 data->fnPosCursor(data);
335                 if (WINE_TRACE_ON(wineconsole))
336                     WINE_DPRINTF(" h-scroll(%d)", evts[i].u.display.left);
337             }
338             if (evts[i].u.display.top != data->curcfg.win_pos.Y)
339             {
340                 data->fnScroll(data, evts[i].u.display.top, FALSE);
341                 data->fnPosCursor(data);
342                 if (WINE_TRACE_ON(wineconsole))
343                     WINE_DPRINTF(" v-scroll(%d)", evts[i].u.display.top);
344             }
345             if (evts[i].u.display.width != data->curcfg.win_width ||
346                 evts[i].u.display.height != data->curcfg.win_height)
347             {
348                 if (WINE_TRACE_ON(wineconsole))
349                     WINE_DPRINTF(" win-size(%d,%d)", evts[i].u.display.width, evts[i].u.display.height);
350                 data->curcfg.win_width = evts[i].u.display.width;
351                 data->curcfg.win_height = evts[i].u.display.height;
352                 data->fnComputePositions(data);
353             }
354             break;
355         case CONSOLE_RENDERER_EXIT_EVENT:
356             if (WINE_TRACE_ON(wineconsole)) WINE_DPRINTF(". Exit!!\n");
357             return 0;
358         default:
359             WINE_FIXME("Unknown event type (%d)\n", evts[i].event);
360         }
361     }
362
363     if (WINE_TRACE_ON(wineconsole)) WINE_DPRINTF(".\n");
364     return 1;
365 }
366
367 /******************************************************************
368  *              WINECON_SetConfig
369  *
370  * Apply to data all the configuration elements from cfg. This includes modification
371  * of server side equivalent and visual parts.
372  * If force is FALSE, only the changed items are modified.
373  */
374 void     WINECON_SetConfig(struct inner_data* data,
375                            const struct config_data* cfg, BOOL force)
376 {
377     if (force || data->curcfg.cursor_size != cfg->cursor_size ||
378         data->curcfg.cursor_visible != cfg->cursor_visible)
379     {
380         CONSOLE_CURSOR_INFO cinfo;
381         cinfo.dwSize = cfg->cursor_size;
382         /* <FIXME>: this hack is needed to pass thru the invariant test operation on server side
383          * (no notification is sent when invariant operation is requested
384          */
385         cinfo.bVisible = !cfg->cursor_visible;
386         SetConsoleCursorInfo(data->hConOut, &cinfo);
387         /* </FIXME> */
388         cinfo.bVisible = cfg->cursor_visible;
389         /* this shall update (through notif) curcfg */
390         SetConsoleCursorInfo(data->hConOut, &cinfo);
391     }
392     if (force || data->curcfg.history_size != cfg->history_size)
393     {
394         data->curcfg.history_size = cfg->history_size;
395         WINECON_SetHistorySize(data->hConIn, cfg->history_size);
396     }
397     if (force || data->curcfg.history_nodup != cfg->history_nodup)
398     {
399         data->curcfg.history_nodup = cfg->history_nodup;
400         WINECON_SetHistoryMode(data->hConIn, cfg->history_nodup);
401     }
402     data->curcfg.menu_mask = cfg->menu_mask;
403     data->curcfg.quick_edit = cfg->quick_edit;
404     if (force || 1 /* FIXME: font info has changed */)
405     {
406         data->fnSetFont(data, cfg->face_name, cfg->cell_height, cfg->font_weight);
407     }
408     if (force || data->curcfg.def_attr != cfg->def_attr)
409     {
410         data->curcfg.def_attr = cfg->def_attr;
411         SetConsoleTextAttribute(data->hConOut, cfg->def_attr);
412     }
413     if (force || data->curcfg.sb_width != cfg->sb_width ||
414         data->curcfg.sb_height != cfg->sb_height)
415     {
416         COORD       c;
417
418         c.X = cfg->sb_width;
419         c.Y = cfg->sb_height;
420
421         /* this shall update (through notif) curcfg */
422         SetConsoleScreenBufferSize(data->hConOut, c);
423     }
424     if (force || data->curcfg.win_width != cfg->win_width ||
425         data->curcfg.win_height != cfg->win_height)
426     {
427         SMALL_RECT  pos;
428
429         pos.Left = pos.Top = 0;
430         pos.Right = cfg->win_width - 1;
431         pos.Bottom = cfg->win_height - 1;
432         /* this shall update (through notif) curcfg */
433         SetConsoleWindowInfo(data->hConOut, FALSE, &pos);
434     }
435     data->curcfg.exit_on_die = cfg->exit_on_die;
436     if (force || data->curcfg.edition_mode != cfg->edition_mode)
437     {
438         data->curcfg.edition_mode = cfg->edition_mode;
439         WINECON_SetEditionMode(data->hConIn, cfg->edition_mode);
440     }
441     /* we now need to gather all events we got from the operations above,
442      * in order to get data correctly updated
443      */
444     WINECON_GrabChanges(data);
445 }
446
447 /******************************************************************
448  *              WINECON_Delete
449  *
450  * Destroy wineconsole internal data
451  */
452 static void WINECON_Delete(struct inner_data* data)
453 {
454     if (!data) return;
455
456     if (data->fnDeleteBackend)  data->fnDeleteBackend(data);
457     if (data->hConIn)           CloseHandle(data->hConIn);
458     if (data->hConOut)          CloseHandle(data->hConOut);
459     if (data->hSynchro)         CloseHandle(data->hSynchro);
460     if (data->cells)            HeapFree(GetProcessHeap(), 0, data->cells);
461     HeapFree(GetProcessHeap(), 0, data);
462 }
463
464 /******************************************************************
465  *              WINECON_Init
466  *
467  * Initialisation part I. Creation of server object (console input and
468  * active screen buffer)
469  */
470 static struct inner_data* WINECON_Init(HINSTANCE hInst, DWORD pid, LPCWSTR appname,
471                                        BOOL (*backend)(struct inner_data*))
472 {
473     struct inner_data*  data = NULL;
474     DWORD               ret;
475     struct config_data  cfg;
476     STARTUPINFOW        si;
477
478     data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data));
479     if (!data) return 0;
480
481     GetStartupInfo(&si);
482
483     if (pid == 0)
484     {
485         if (!si.lpTitle) WINECON_Fatal("Should have a title set");
486         appname = si.lpTitle;
487     }
488
489     /* load settings */
490     WINECON_RegLoad(appname, &cfg);
491
492     /* some overrides */
493     if (pid == 0)
494     {
495         if (si.dwFlags & STARTF_USECOUNTCHARS)
496         {
497             cfg.sb_width  = si.dwXCountChars;
498             cfg.sb_height = si.dwYCountChars;
499         }
500         if (si.dwFlags & STARTF_USEFILLATTRIBUTE)
501             cfg.def_attr = si.dwFillAttribute;
502         /* should always be defined */
503     }
504
505     /* the handles here are created without the whistles and bells required by console
506      * (mainly because wineconsole doesn't need it)
507      * - they are not inheritable
508      * - hConIn is not synchronizable
509      */
510     SERVER_START_REQ(alloc_console)
511     {
512         req->access  = GENERIC_READ | GENERIC_WRITE;
513         req->inherit = FALSE;
514         req->pid     = pid;
515         ret = !wine_server_call_err( req );
516         data->hConIn = (HANDLE)reply->handle_in;
517         data->hSynchro = (HANDLE)reply->event;
518     }
519     SERVER_END_REQ;
520     if (!ret) goto error;
521     WINE_TRACE("using hConIn %p, hSynchro event %p\n", data->hConIn, data->hSynchro);
522
523     SERVER_START_REQ( set_console_input_info )
524     {
525         req->handle = (obj_handle_t)data->hConIn;
526         req->mask = SET_CONSOLE_INPUT_INFO_TITLE;
527         wine_server_add_data( req, appname, lstrlenW(appname) * sizeof(WCHAR) );
528         ret = !wine_server_call_err( req );
529     }
530     SERVER_END_REQ;
531     if (!ret) goto error;
532
533     SERVER_START_REQ(create_console_output)
534     {
535         req->handle_in = (obj_handle_t)data->hConIn;
536         req->access    = GENERIC_WRITE|GENERIC_READ;
537         req->share     = FILE_SHARE_READ|FILE_SHARE_WRITE;
538         req->inherit   = FALSE;
539         ret = !wine_server_call_err( req );
540         data->hConOut  = (HANDLE)reply->handle_out;
541     }
542     SERVER_END_REQ;
543     if (!ret) goto error;
544     WINE_TRACE("using hConOut %p\n", data->hConOut);
545
546     /* filling data->curcfg from cfg */
547     if ((*backend)(data))
548     {
549         WINECON_SetConfig(data, &cfg, TRUE);
550         data->curcfg.registry = cfg.registry;
551         WINECON_DumpConfig("fint", &data->curcfg);
552         return data;
553     }
554
555  error:
556     WINE_ERR("failed to init.\n");
557
558     WINECON_Delete(data);
559     return NULL;
560 }
561
562 /******************************************************************
563  *              WINECON_Spawn
564  *
565  * Spawn the child process when invoked with wineconsole foo bar
566  */
567 static BOOL WINECON_Spawn(struct inner_data* data, LPWSTR cmdLine)
568 {
569     PROCESS_INFORMATION info;
570     STARTUPINFO         startup;
571     BOOL                done;
572
573     /* we're in the case wineconsole <exe> <options>... spawn the new process */
574     memset(&startup, 0, sizeof(startup));
575     startup.cb          = sizeof(startup);
576     startup.dwFlags     = STARTF_USESTDHANDLES;
577
578     /* the attributes of wineconsole's handles are not adequate for inheritance, so
579      * get them with the correct attributes before process creation
580      */
581     if (!DuplicateHandle(GetCurrentProcess(), data->hConIn,  GetCurrentProcess(),
582                          &startup.hStdInput, GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE, TRUE, 0) ||
583         !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
584                          &startup.hStdOutput, GENERIC_READ|GENERIC_WRITE, TRUE, 0) ||
585         !DuplicateHandle(GetCurrentProcess(), data->hConOut, GetCurrentProcess(),
586                          &startup.hStdError, GENERIC_READ|GENERIC_WRITE, TRUE, 0))
587     {
588         WINE_ERR("Can't dup handles\n");
589         /* no need to delete handles, we're exiting the programm anyway */
590         return FALSE;
591     }
592
593     done = CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, 0L, NULL, NULL, &startup, &info);
594
595     /* we no longer need the handles passed to the child for the console */
596     CloseHandle(startup.hStdInput);
597     CloseHandle(startup.hStdOutput);
598     CloseHandle(startup.hStdError);
599
600     return done;
601 }
602
603 /******************************************************************
604  *               WINECON_HasEvent
605  *
606  *
607  */
608 static BOOL WINECON_HasEvent(LPCSTR ptr, unsigned* evt)
609 {
610     while (*ptr == ' ' || *ptr == '\t') ptr++;
611     if (strncmp(ptr, "--use-event=", 12)) return FALSE;
612     return sscanf(ptr + 12, "%d", evt) == 1;
613 }
614
615 /******************************************************************
616  *              WinMain
617  *
618  * wineconsole can either be started as:
619  *      wineconsole --use-event=<int>   used when a new console is created (AllocConsole)
620  *      wineconsole <pgm> <arguments>   used to start the program <pgm> from the command line in
621  *                                      a freshly created console
622  */
623 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, INT nCmdShow)
624 {
625     struct inner_data*  data;
626     int                 ret = 1;
627     unsigned            evt;
628
629     /* case of wineconsole <evt>, signal process that created us that we're up and running */
630     if (WINECON_HasEvent(lpCmdLine, &evt))
631     {
632         if (!(data = WINECON_Init(hInst, 0, NULL, WCUSER_InitBackend))) return 0;
633         ret = SetEvent((HANDLE)evt);
634         if (!ret)
635         {
636             WINE_ERR("SetEvent failed.\n");
637             goto cleanup;
638         }
639     }
640     else
641     {
642         LPWSTR          wcmdLine = GetCommandLine() /* we're unicode... */;
643         LPWSTR          src, dst;
644         WCHAR           buffer[256];
645
646         /* remove wineconsole from commandline...
647          * we could have several ' ' in process command line... so try first space...
648          */
649         /* FIXME:
650          * the correct way would be to check the existence of the left part of ptr
651          * (to be a file)
652          */
653         /* FIXME: could also add an option to choose another backend if needed */
654         while (*wcmdLine && *wcmdLine++ != ' ');
655
656         /* FIXME: see above */
657         src = wcmdLine; dst = buffer;
658         while (*src && *src != ' ') *dst++ = *src++;
659         *dst = 0;
660
661         if (!(data = WINECON_Init(hInst, GetCurrentProcessId(), buffer, WCCURSES_InitBackend))) return 0;
662         ret = WINECON_Spawn(data, wcmdLine);
663         if (!ret)
664         {
665             WINE_MESSAGE("wineconsole: spawning client program failed. Invalid/missing command line arguments ?\n");
666             goto cleanup;
667         }
668     }
669
670     if (ret)
671     {
672         WINE_TRACE("calling MainLoop.\n");
673         ret = data->fnMainLoop(data);
674     }
675
676 cleanup:
677     WINECON_Delete(data);
678
679     return ret;
680 }