aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ecore/ecore_exe_win32.c
blob: 44ee08f39e8af4bb7343219195ec8634fde64123 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#include <process.h>

#include "Ecore.h"
#include "ecore_private.h"

#include "ecore_exe_private.h"

/*
 * TESTS
 *
 * [X] add event
 * [X] data event
 * [X] error event
 * [X] data event buffered
 * [X] del event
 * [ ] batch files
 * [X] exit code
 * [X] inherited env var
 */

/*
 * FIXME :
 *
 * [ ] child program with ecore main loop does not exit and nothing is sent
 * [ ] ecore_exe_send fails (race condition ? same problem as above ?)
 */

#define ECORE_EXE_WIN32_TIMEOUT 3000

static int run_pri = NORMAL_PRIORITY_CLASS;

static void
_ecore_exe_threads_terminate(Ecore_Exe *obj)
{
   Ecore_Exe_Data *exe = efl_data_scope_get(obj, ECORE_EXE_CLASS);
   HANDLE threads[2] = { NULL, NULL };
   int i = 0;

   if (exe->pipe_read.thread)
     {
        threads[i] = exe->pipe_read.thread;
        i++;
     }
   if (exe->pipe_error.thread)
     {
        threads[i] = exe->pipe_error.thread;
        i++;
     }
   if (i > 0)
     {
        exe->close_threads = 1;
        WaitForMultipleObjects(i, threads, TRUE, INFINITE);
	IF_FN_DEL(CloseHandle, exe->pipe_error.thread);
	IF_FN_DEL(CloseHandle, exe->pipe_read.thread);
     }
}

static Eina_Bool
_ecore_exe_close_cb(void *data,
                    Ecore_Win32_Handler *wh EINA_UNUSED)
{
   Ecore_Exe_Event_Del *e;
   Ecore_Exe *obj = data;
   Ecore_Exe_Data *exe = efl_data_scope_get(obj, ECORE_EXE_CLASS);
   DWORD exit_code = 0;

   _ecore_exe_threads_terminate(obj);

   e = calloc(1, sizeof(Ecore_Exe_Event_Del));
   if (!e) return 0;

   /* FIXME : manage the STILL_ACTIVE returned error */
   if (!GetExitCodeProcess(exe->process, &exit_code))
     {
        char *msg;

        msg = evil_last_error_get();
        printf("%s\n", msg);
        free(msg);
     }

   e->exit_code = exit_code;
   e->exited = 1;
   e->pid = exe->pid;
   e->exe = obj;
   exe->h_close = NULL; // It's going to get deleted in the next callback.

   ecore_event_add(ECORE_EXE_EVENT_DEL, e,
                   _ecore_exe_event_del_free, NULL);

   DBG("Exiting process %s with exit code %d\n", exe->cmd, e->exit_code);

   return 0;
}

static unsigned int __stdcall
_ecore_exe_pipe_read_thread_cb(void *data)
{
   char buf[64];
   Ecore_Exe *obj = data;
   Ecore_Exe_Data *exe = efl_data_scope_get(obj, ECORE_EXE_CLASS);
   Ecore_Exe_Event_Data *event_data;
   char *current_buf = NULL;
   DWORD size;
   DWORD current_size = 0;
   BOOL res;

   while (1)
     {
        res = PeekNamedPipe(exe->pipe_read.child_pipe,
                            buf, sizeof(buf) - 1, &size, &current_size, NULL);
        if (!res || (size == 0))
          {
             if (exe->close_threads)
               break;

             Sleep(100);
             continue;
          }

        current_buf = (char *)malloc(current_size);
        if (!current_buf)
          continue;

        res = ReadFile(exe->pipe_read.child_pipe, current_buf, current_size, &size, NULL);
        if (!res || (size == 0))
          {
             free(current_buf);
             current_buf = NULL;
             continue;
          }

        current_size = size;

        if (!exe->pipe_read.data_buf)
          {
             exe->pipe_read.data_buf = current_buf;
             exe->pipe_read.data_size = current_size;
          }
        else
          {
             exe->pipe_read.data_buf = realloc(exe->pipe_read.data_buf, exe->pipe_read.data_size + current_size);
             memcpy((unsigned char *)exe->pipe_read.data_buf + exe->pipe_read.data_size, current_buf, current_size);
             exe->pipe_read.data_size += current_size;
             free(current_buf);
          }

        event_data = ecore_exe_event_data_get(obj, ECORE_EXE_PIPE_READ);
        if (event_data)
          {
             ecore_event_add(ECORE_EXE_EVENT_DATA, event_data,
                             _ecore_exe_event_exe_data_free,
                             NULL);
             efl_event_callback_legacy_call(obj, ECORE_EXE_EVENT_DATA_GET, event_data);
          }

        current_buf = NULL;
        current_size = 0;
     }

   _endthreadex(0);

   return 0;
}

static unsigned int __stdcall
_ecore_exe_pipe_error_thread_cb(void *data)
{
   char buf[64];
   Ecore_Exe *obj = data;
   Ecore_Exe_Data *exe = efl_data_scope_get(obj, ECORE_EXE_CLASS);
   Ecore_Exe_Event_Data *event_data;
   char *current_buf = NULL;
   DWORD size;
   DWORD current_size = 0;
   BOOL res;

   while (1)
     {
        res = PeekNamedPipe(exe->pipe_error.child_pipe,
                            buf, sizeof(buf) - 1, &size, &current_size, NULL);
        if (!res || (size == 0))
          {
             if (exe->close_threads)
               break;

             Sleep(100);
             continue;
          }

        current_buf = (char *)malloc(current_size);
        if (!current_buf)
          continue;

        res = ReadFile(exe->pipe_error.child_pipe, current_buf, current_size, &size, NULL);
        if (!res || (size == 0))
          {
             free(current_buf);
             current_buf = NULL;
             continue;
          }

        current_size = size;

        if (!exe->pipe_error.data_buf)
          {
             exe->pipe_error.data_buf = current_buf;
             exe->pipe_error.data_size = current_size;
          }
        else
          {
             exe->pipe_error.data_buf = realloc(exe->pipe_error.data_buf, exe->pipe_error.data_size + current_size);
             memcpy((unsigned char *)exe->pipe_error.data_buf + exe->pipe_error.data_size, current_buf, current_size);
             exe->pipe_error.data_size += current_size;
             free(current_buf);
          }

        event_data = ecore_exe_event_data_get(obj, ECORE_EXE_PIPE_ERROR);
        if (event_data)
          {
             ecore_event_add(ECORE_EXE_EVENT_ERROR, event_data,
                             _ecore_exe_event_exe_data_free,
                             NULL);
             efl_event_callback_legacy_call(obj, ECORE_EXE_EVENT_DATA_ERROR, event_data);
          }

        current_buf = NULL;
        current_size = 0;
     }

   _endthreadex(0);

   return 0;
}

static DWORD WINAPI
_ecore_exe_thread_procedure(LPVOID data EINA_UNUSED)
{
   GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
   GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, 0);
   return 1;
}

static BOOL CALLBACK
_ecore_exe_enum_windows_procedure(HWND window,
                                  LPARAM data)
{
   Ecore_Exe *obj = (Ecore_Exe *) data;
   Ecore_Exe_Data *exe = efl_data_scope_get(obj, ECORE_EXE_CLASS);
   DWORD thread_id;

   thread_id = GetWindowThreadProcessId(window, NULL);

   if (thread_id == exe->thread_id)
     {
        /* Ctrl-C or Ctrl-Break */
        if (CreateRemoteThread(exe->process, NULL, 0,
                               (LPTHREAD_START_ROUTINE)_ecore_exe_thread_procedure, NULL,
                                0, NULL))
          {
             printf ("remote thread\n");
             return EINA_FALSE;
          }

        if ((exe->sig == ECORE_EXE_WIN32_SIGINT) ||
            (exe->sig == ECORE_EXE_WIN32_SIGQUIT))
          {
             printf ("int or quit\n");
             return EINA_FALSE;
          }

        /* WM_CLOSE message */
        PostMessage(window, WM_CLOSE, 0, 0);
        if (WaitForSingleObject(exe->process, ECORE_EXE_WIN32_TIMEOUT) == WAIT_OBJECT_0)
          {
             printf ("CLOSE\n");
             return EINA_FALSE;
          }

        /* WM_QUIT message */
        PostMessage(window, WM_QUIT, 0, 0);
        if (WaitForSingleObject(exe->process, ECORE_EXE_WIN32_TIMEOUT) == WAIT_OBJECT_0)
          {
             printf ("QUIT\n");
             return EINA_FALSE;
          }

        /* Exit process */
        if (CreateRemoteThread(exe->process, NULL, 0,
                               (LPTHREAD_START_ROUTINE)ExitProcess, NULL,
                               0, NULL))
          {
             printf ("remote thread 2\n");
             return EINA_FALSE;
          }

        if (exe->sig == ECORE_EXE_WIN32_SIGTERM)
          {
             printf ("term\n");
             return EINA_FALSE;
          }

        TerminateProcess(exe->process, 0);

        return EINA_FALSE;
     }

   return EINA_TRUE;
}

void
_impl_ecore_exe_run_priority_set(int pri)
{
   switch (pri)
     {
      case ECORE_EXE_WIN32_PRIORITY_IDLE:
        run_pri = IDLE_PRIORITY_CLASS;
        break;

      case ECORE_EXE_WIN32_PRIORITY_BELOW_NORMAL:
        run_pri = BELOW_NORMAL_PRIORITY_CLASS;
        break;

      case ECORE_EXE_WIN32_PRIORITY_NORMAL:
        run_pri = NORMAL_PRIORITY_CLASS;
        break;

      case ECORE_EXE_WIN32_PRIORITY_ABOVE_NORMAL:
        run_pri = ABOVE_NORMAL_PRIORITY_CLASS;
        break;

      case ECORE_EXE_WIN32_PRIORITY_HIGH:
        run_pri = HIGH_PRIORITY_CLASS;
        break;

      case ECORE_EXE_WIN32_PRIORITY_REALTIME:
        run_pri = REALTIME_PRIORITY_CLASS;
        break;

      default:
        break;
     }
}

int
_impl_ecore_exe_run_priority_get(void)
{
   switch (run_pri)
     {
      case IDLE_PRIORITY_CLASS:
        return ECORE_EXE_WIN32_PRIORITY_IDLE;

      case BELOW_NORMAL_PRIORITY_CLASS:
        return ECORE_EXE_WIN32_PRIORITY_BELOW_NORMAL;

      case NORMAL_PRIORITY_CLASS:
        return ECORE_EXE_WIN32_PRIORITY_NORMAL;

      case ABOVE_NORMAL_PRIORITY_CLASS:
        return ECORE_EXE_WIN32_PRIORITY_ABOVE_NORMAL;

      case HIGH_PRIORITY_CLASS:
        return ECORE_EXE_WIN32_PRIORITY_HIGH;

      case REALTIME_PRIORITY_CLASS:
        return ECORE_EXE_WIN32_PRIORITY_REALTIME;

      /* default should not be reached */
      default:
        return ECORE_EXE_WIN32_PRIORITY_NORMAL;
     }
}

Eo *
_impl_ecore_exe_efl_object_finalize(Eo *obj, Ecore_Exe_Data *exe)
{
   char exe_cmd_buf[32768];
   SECURITY_ATTRIBUTES sa;
   STARTUPINFO si;
   PROCESS_INFORMATION pi;
   HANDLE child_pipe_read = NULL;
   HANDLE child_pipe_error = NULL;
   const char *shell = NULL;
   Ecore_Exe_Event_Add *e;
   Ecore_Exe_Flags flags;
   Eina_Bool use_sh = EINA_FALSE;

   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);

   flags = exe->flags;

   DBG("Creating process %s with flags %d", exe->cmd, flags);

   if (!exe->cmd) goto error;

   if ((flags & ECORE_EXE_PIPE_AUTO) && (!(flags & ECORE_EXE_PIPE_ERROR))
       && (!(flags & ECORE_EXE_PIPE_READ)))
     /* We need something to auto pipe. */
     flags |= ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_ERROR;

   if ((flags & ECORE_EXE_USE_SH))
     use_sh = EINA_TRUE;
   else
     use_sh = eina_str_has_extension(exe->cmd, ".bat");

   if (use_sh)
     {
        int len;

        shell = "cmd.exe";
        len = snprintf(exe_cmd_buf, sizeof(exe_cmd_buf), "/c %s", exe->cmd);
        if (len >= (int)sizeof(exe_cmd_buf))
          exe_cmd_buf[sizeof(exe_cmd_buf) - 1] = '\0';
     }
   else
     {
        int len;

        /* FIXME : faster with memset() but one must be careful with size */
        len = snprintf(exe_cmd_buf, sizeof(exe_cmd_buf), "%s", exe->cmd);
        if (len >= (int)sizeof(exe_cmd_buf))
          exe_cmd_buf[sizeof(exe_cmd_buf) - 1] = '\0';
     }

   /* stdout, stderr and stdin pipes */

   sa.nLength = sizeof(SECURITY_ATTRIBUTES);
   sa.bInheritHandle = EINA_TRUE;
   sa.lpSecurityDescriptor = NULL;

   /* stdout pipe */
   if (exe->flags & ECORE_EXE_PIPE_READ)
     {
        if (!CreatePipe(&exe->pipe_read.child_pipe, &child_pipe_read, &sa, 0))
          goto error;
        if (!SetHandleInformation(exe->pipe_read.child_pipe, HANDLE_FLAG_INHERIT, 0))
          goto error;
        exe->pipe_read.thread = (HANDLE)_beginthreadex(NULL, 0,
                                                       _ecore_exe_pipe_read_thread_cb,
                                                       obj, 0, NULL);
        if (!exe->pipe_read.thread)
          goto error;
     }

   /* stderr pipe */
   if (exe->flags & ECORE_EXE_PIPE_ERROR)
     {
        if (!CreatePipe(&exe->pipe_error.child_pipe, &child_pipe_error, &sa, 0))
          goto error;
        if (!SetHandleInformation(exe->pipe_error.child_pipe, HANDLE_FLAG_INHERIT, 0))
          goto error;
        exe->pipe_error.thread = (HANDLE)_beginthreadex(NULL, 0,
                                                        _ecore_exe_pipe_error_thread_cb,
                                                        obj, 0, NULL);
        if (!exe->pipe_error.thread)
          goto error;
     }

   /* stdin pipe */
   if (exe->flags & ECORE_EXE_PIPE_WRITE)
     {
        if (!CreatePipe(&exe->pipe_write.child_pipe, &exe->pipe_write.child_pipe_x, &sa, 0))
          goto error;
        if (!SetHandleInformation(exe->pipe_write.child_pipe_x, HANDLE_FLAG_INHERIT, 0))
          goto error;
     }

   /* create child process */

   ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));

   ZeroMemory(&si, sizeof(STARTUPINFO));
   si.cb = sizeof(STARTUPINFO);
   si.hStdOutput = child_pipe_read;
   si.hStdInput = exe->pipe_write.child_pipe;
   si.hStdError = child_pipe_error;
   si.dwFlags |= STARTF_USESTDHANDLES;

   DBG("CreateProcess: shell:%s child:%s", use_sh ? "yes" : "no", exe_cmd_buf);
   if (!CreateProcess(shell, exe_cmd_buf, NULL, NULL, EINA_TRUE,
                      run_pri | CREATE_SUSPENDED, NULL, NULL, &si, &pi))
     {
        char *msg;

	msg = evil_last_error_get();
	if (msg)
	  {
	     WRN("Failed to create process %s: %s", exe_cmd_buf, msg);
	     free(msg);
	  }
	else
	  WRN("Failed to create process %s: %ld", exe_cmd_buf, GetLastError());
        goto error;
     }

   /*
    * Close pipe handles (do not continue to modify the parent).
    * We need to make sure that no handles to the write end of the
    * output and error pipes are maintained in this process or else
    * the pipe will not close when the child process exits and the
    * ReadFile will hang.
    */
   IF_FN_DEL(CloseHandle, child_pipe_read);
   IF_FN_DEL(CloseHandle, child_pipe_error);

   /* be sure that the child process is running */
   /* FIXME: This does not work if the child is an EFL-based app */
   /* if (WaitForInputIdle(pi.hProcess, INFINITE) == WAIT_FAILED) */
   /*   goto error; */

   exe->process = pi.hProcess;
   exe->process_thread = pi.hThread;
   exe->pid = pi.dwProcessId;
   exe->thread_id = pi.dwThreadId;

   exe->h_close = ecore_main_win32_handler_add(exe->process,
                                               _ecore_exe_close_cb, obj);
   if (!exe->h_close)
     goto error;

   if (ResumeThread(exe->process_thread) == ((DWORD)-1))
     {
        ERR("Could not resume process");
        goto error;
     }

   _ecore_exe_exes = eina_list_append(_ecore_exe_exes, obj);

   e = (Ecore_Exe_Event_Add *)calloc(1, sizeof(Ecore_Exe_Event_Add));
   if (!e) goto error;

   e->exe = obj;

   ecore_event_add(ECORE_EXE_EVENT_ADD, e,
                   _ecore_exe_event_add_free, NULL);

   return obj;

error:
   return NULL;
}

Eina_Bool
_impl_ecore_exe_send(Ecore_Exe  *obj,
               Ecore_Exe_Data *exe,
               const void *data,
               int         size)
{
   void *buf = NULL;
   DWORD num_exe;
   BOOL res;

   buf = realloc(exe->pipe_write.data_buf, exe->pipe_write.data_size + size);
   if (!buf) return EINA_FALSE;

   exe->pipe_write.data_buf = buf;
   memcpy((char *)exe->pipe_write.data_buf + exe->pipe_write.data_size, data, size);
   exe->pipe_write.data_size += size;

   res = WriteFile(exe->pipe_write.child_pipe_x, buf, READBUFSIZ, &num_exe, NULL);
   printf(" ** res : %d\n", res);
   if (!res || num_exe == 0)
     {
        ERR("Ecore_Exe %p stdin is closed! Cannot send %d bytes from %p",
            obj, size, data);
        return EINA_FALSE;
     }

   return EINA_TRUE;
}

Ecore_Exe_Event_Data *
_impl_ecore_exe_event_data_get(Ecore_Exe      *obj,
                         Ecore_Exe_Data *exe,
                         Ecore_Exe_Flags flags)
{
   Ecore_Exe_Event_Data *e = NULL;
   unsigned char *inbuf;
   DWORD inbuf_num;
   Eina_Bool is_buffered = EINA_FALSE;

   /* Sort out what sort of event we are. */
   if (flags & ECORE_EXE_PIPE_READ)
   {
      flags = ECORE_EXE_PIPE_READ;
      if (exe->flags & ECORE_EXE_PIPE_READ_LINE_BUFFERED)
        is_buffered = EINA_TRUE;
   }
   else
   {
      flags = ECORE_EXE_PIPE_ERROR;
      if (exe->flags & ECORE_EXE_PIPE_ERROR_LINE_BUFFERED)
        is_buffered = EINA_TRUE;
   }

   /* Get the data. */
   if (flags & ECORE_EXE_PIPE_READ)
   {
      inbuf = exe->pipe_read.data_buf;
      inbuf_num = exe->pipe_read.data_size;
      exe->pipe_read.data_buf = NULL;
      exe->pipe_read.data_size = 0;
   }
   else
   {
      inbuf = exe->pipe_error.data_buf;
      inbuf_num = exe->pipe_error.data_size;
      exe->pipe_error.data_buf = NULL;
      exe->pipe_error.data_size = 0;
   }

   e = calloc(1, sizeof(Ecore_Exe_Event_Data));
   if (e)
   {
      e->exe = obj;
      e->data = inbuf;
      e->size = inbuf_num;

      if (is_buffered) /* Deal with line buffering. */
      {
         char *c;
         DWORD i;
         DWORD max = 0;
         DWORD count = 0;
         DWORD last = 0;

         c = (char *)inbuf;
         for (i = 0; i < inbuf_num; i++)
           {
              if (inbuf[i] == '\n')
                {
                   int end;

                   if (count >= max)
                     {
                        max += 10;
                        e->lines = realloc(e->lines, sizeof(Ecore_Exe_Event_Data_Line) * (max + 1));
                     }

                   if ((i >= 1) && (inbuf[i - 1] == '\r'))
                     end = i - 1;
                   else
                     end = i;
                   inbuf[end] = '\0';
                   e->lines[count].line = c;
                   e->lines[count].size = end - last;
                   last = i + 1;
                   c = (char *)&inbuf[last];
                   count++;
                }
           }
         if (i > last) /* Partial line left over, save it for next time. */
           {
              if (count != 0) e->size = last;
              if (flags & ECORE_EXE_PIPE_READ)
                {
                   exe->pipe_read.data_size = i - last;
                   exe->pipe_read.data_buf = malloc(exe->pipe_read.data_size);
                   memcpy(exe->pipe_read.data_buf, c, exe->pipe_read.data_size);
                }
              else
                {
                  exe->pipe_error.data_size = i - last;
                  exe->pipe_error.data_buf = malloc(exe->pipe_error.data_size);
                  memcpy(exe->pipe_error.data_buf, c, exe->pipe_error.data_size);
                }
           }
         if (count == 0) /* No lines to send, cancel the event. */
           {
               _ecore_exe_event_exe_data_free(NULL, e);
               e = NULL;
           }
         else /* NULL terminate the array, so that people know where the end is. */
           {
              e->lines[count].line = NULL;
              e->lines[count].size = 0;
           }
      }
   }

   return e;
}

void
_impl_ecore_exe_efl_object_destructor(Eo *obj, Ecore_Exe_Data *exe)
{
   void *data;

   _ecore_exe_threads_terminate(obj);

   data = exe->data;
   if (exe->pre_free_cb)
     exe->pre_free_cb(data, obj);

   IF_FN_DEL(ecore_main_win32_handler_del, exe->h_close);

   IF_FN_DEL(CloseHandle, exe->process_thread);
   IF_FN_DEL(CloseHandle, exe->process);
   IF_FN_DEL(CloseHandle, exe->pipe_write.child_pipe);
   IF_FN_DEL(CloseHandle, exe->pipe_write.child_pipe_x);
   IF_FN_DEL(CloseHandle, exe->pipe_error.child_pipe);
   IF_FN_DEL(CloseHandle, exe->pipe_read.child_pipe);

   IF_FREE(exe->cmd);

   _ecore_exe_exes = eina_list_remove(_ecore_exe_exes, obj);
   IF_FREE(exe->tag);
}

void
_impl_ecore_exe_pause(Ecore_Exe *obj EINA_UNUSED, Ecore_Exe_Data *exe)
{
   if (exe->is_suspended)
     return;

   if (SuspendThread(exe->process_thread) != (DWORD)-1)
     exe->is_suspended = 1;
}

void
_impl_ecore_exe_continue(Ecore_Exe *obj EINA_UNUSED, Ecore_Exe_Data *exe)
{
   if (!exe->is_suspended)
     return;

   if (ResumeThread(exe->process_thread) != (DWORD)-1)
     exe->is_suspended = 0;
}

void
_impl_ecore_exe_interrupt(Ecore_Exe *obj, Ecore_Exe_Data *exe)
{
   CloseHandle(exe->process_thread);
   exe->process_thread = NULL;
   CloseHandle(exe->process);
   exe->process = NULL;
   exe->sig = ECORE_EXE_WIN32_SIGINT;
   EnumWindows(_ecore_exe_enum_windows_procedure, (LPARAM)obj);
}

void
_impl_ecore_exe_quit(Ecore_Exe *obj, Ecore_Exe_Data *exe)
{
   CloseHandle(exe->process_thread);
   exe->process_thread = NULL;
   CloseHandle(exe->process);
   exe->process = NULL;
   exe->sig = ECORE_EXE_WIN32_SIGQUIT;
   EnumWindows(_ecore_exe_enum_windows_procedure, (LPARAM)obj);
}

void
_impl_ecore_exe_terminate(Ecore_Exe *obj, Ecore_Exe_Data *exe)
{
/*    CloseHandle(exe->thread); */
   CloseHandle(exe->process);
   exe->process = NULL;
   exe->sig = ECORE_EXE_WIN32_SIGTERM;
   while (EnumWindows(_ecore_exe_enum_windows_procedure, (LPARAM)obj)) ;
}

void
_impl_ecore_exe_kill(Ecore_Exe *obj, Ecore_Exe_Data *exe)
{
   CloseHandle(exe->process_thread);
   exe->process_thread = NULL;
   CloseHandle(exe->process);
   exe->process = NULL;
   exe->sig = ECORE_EXE_WIN32_SIGKILL;
   while (EnumWindows(_ecore_exe_enum_windows_procedure, (LPARAM)obj)) ;
}

void
_impl_ecore_exe_auto_limits_set(Ecore_Exe *obj EINA_UNUSED,
                          Ecore_Exe_Data *exe EINA_UNUSED,
                          int        start_bytes EINA_UNUSED,
                          int        end_bytes EINA_UNUSED,
                          int        start_lines EINA_UNUSED,
                          int        end_lines EINA_UNUSED)
{
   ERR("Not implemented on windows!");
}

void
_impl_ecore_exe_signal(Ecore_Exe *obj EINA_UNUSED,
                          Ecore_Exe_Data *exe EINA_UNUSED,
                          int num EINA_UNUSED)
{
   ERR("Not implemented on windows!");
}

void
_impl_ecore_exe_hup(Ecore_Exe *obj EINA_UNUSED,
                          Ecore_Exe_Data *exe EINA_UNUSED)
{
   ERR("Not implemented on windows!");
}