summaryrefslogtreecommitdiff
path: root/legacy/ecore/examples/exe_example.c
blob: 81be60fa3e755c086959f56fa66183c3b4cfcc7d (plain)
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
/* Timer example.
 */

#include <Ecore.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int size = 0;
int exe_count = 0;
int data_count = 0;
int line_count = 0;
double then = 0.0, now = 0.0;
Ecore_Exe *exe0 = NULL;
Ecore_Exe *exe1 = NULL;
Ecore_Exe *exe2 = NULL;
Ecore_Exe *exe3 = NULL;

static int
exe_data(void *data, int type, void *event)
{
   Ecore_Event_Exe_Data *ev;
   int i;

   ev = event;
   printf("  [*] DATA RET EXE %p - %p [%i bytes]\n", ev->exe, ev->data, ev->size);

   if (ev->lines)
      {
         int i;
	       
	 for (i = 0; ev->lines[i].line != NULL; i++)
	    {
               printf("%d %s\n", ev->lines[i].size, ev->lines[i].line);
	    }
      }
   else
      {
         for (i = 0; i < ev->size; i++)
            putchar(((unsigned char *)ev->data)[i]);
      }
   printf("\n");
   return 1;
}

static int
exe_data_count(void *data, int type, void *event)
{
   Ecore_Event_Exe_Data *ev;
   int i;

   ev = event;

   if (ev->lines)
      {
	 for (i = 0; ev->lines[i].line != NULL; i++)
	    line_count++;
         /* printf("%d ", i);   This is for testing the number of lines per event.*/
      }

   data_count += ev->size;
   if (data_count >= size)
      {
         now = ecore_time_get();
	 printf("\n\nApproximate data rate - %f bytes/second (%d lines and %d bytes (x2) in %f seconds).\n", ((double) data_count * 2.0) / (now - then), line_count, data_count, now - then);
	 if (data_count != size)
	    printf("Size discrepency of %d bytes.\n", size - data_count);
         printf("\n");

         /* Since there does not seem to be anyway to convince /bin/cat to finish... */
	 ecore_exe_terminate(exe0);
      }

   return 1;
}

static int
exe_exit(void *data, int type, void *event)
{
   Ecore_Event_Exe_Exit *ev;

   ev = event;
   printf("  [*] EXE EXIT: %p\n", ev->exe);
   exe_count--;
   if (exe_count <= 0)
      ecore_main_loop_quit();
   return 1;
}

int timer_once(void *data)
{
   int argc;
   char **argv;
   int i = 1;

   ecore_app_args_get(&argc, &argv);
   ecore_event_handler_add(ECORE_EVENT_EXE_DATA, exe_data_count, NULL);
   printf("FILE : %s\n", argv[i]);
   exe0 = ecore_exe_pipe_run("/bin/cat", ECORE_EXE_PIPE_WRITE | ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_READ_LINE_BUFFERED, NULL);

   if (exe0)
      {
         struct stat s;

	 exe_count++;
         if (stat(argv[i], &s) == 0)
	    {
	       int fd;

               size = s.st_size;
	       if ((fd = open(argv[i], O_RDONLY)) != -1)
	          {
		      char buf[1024];
		      int length;

                      then = ecore_time_get();
		      while ((length = read(fd, buf, 1024)) > 0)
                         ecore_exe_send(exe0, buf, length);
		      close(fd);
		  }
	    }
         /* FIXME: Fuckit, neither of these will actually cause /bin/cat to shut down.  What the fuck does it take? */
         ecore_exe_send(exe0, "\004", 1);  /* Send an EOF. */
         ecore_exe_close_stdin(exe0);  /* /bin/cat should stop when it's stdin closes. */
      }

  return 0;
}

int main(int argc, char **argv) 
{
  ecore_app_args_set(argc, (const char **) argv);

   ecore_init();
   ecore_event_handler_add(ECORE_EVENT_EXE_EXIT, exe_exit, NULL);

   if (argc == 1)
      {
         ecore_event_handler_add(ECORE_EVENT_EXE_DATA, exe_data, NULL);
         exe0 = ecore_exe_run("/bin/uname -a", NULL);
         if (exe0)   exe_count++;

         exe1 = ecore_exe_pipe_run("/bin/sh",
			    ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_WRITE,
			    NULL);
         if (exe1)
	    {
	       exe_count++;
               ecore_exe_send(exe1, "ls\n", 3);
               ecore_exe_send(exe1, "exit\n", 5);
	    }

         exe2 = ecore_exe_pipe_run("/usr/bin/find . -print",
			    ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_READ_LINE_BUFFERED,
			    NULL);
         if (exe2)   exe_count++;

         exe3 = ecore_exe_pipe_run("/bin/cat",
			    ECORE_EXE_PIPE_WRITE,
			    NULL);
         if (exe3)
	    {
	       exe_count++;
               ecore_exe_send(exe3, "ls\n", 3);
	    }

         printf("  [*] exe0 = %p (/bin/uname -a)\n", exe0);
         printf("  [*] exe1 = %p (echo \"ls\" | /bin/sh)\n", exe1);
         printf("  [*] exe2 = %p (/usr/bin/find / -print)\n", exe2);
         printf("  [*] exe3 = %p (echo \"ls\" | /bin/cat)\n", exe3);
      }
   else
      ecore_timer_add(0.5, timer_once, NULL);

   if ((exe_count > 0) || (argc > 1))
      ecore_main_loop_begin();

   ecore_shutdown();
   return 0;
}