summaryrefslogtreecommitdiff
path: root/src/lib/ecore/ecore_exe.c
blob: 75c577b67ff26e503ad0e550277f2c4d2ef6e032 (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
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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <Efl.h>

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

#define MY_CLASS ECORE_EXE_CLASS

#include "ecore_exe_private.h"

/* TODO: Something to let people build a command line and does auto escaping -
 *
 * ecore_exe_snprintf()
 *
 *   OR
 *
 * cmd = ecore_exe_comand_parameter_append(cmd, "firefox");
 * cmd = ecore_exe_comand_parameter_append(cmd, "http://www.foo.com/bar.html?baz=yes");
 * each parameter appended is one argument, and it gets escaped, quoted, and
 * appended with a preceding space.  The first is the command off course.
 */

struct _ecore_exe_dead_exe
{
   pid_t pid;
   char *cmd;
};

EAPI int ECORE_EXE_EVENT_ADD = 0;
EAPI int ECORE_EXE_EVENT_DEL = 0;
EAPI int ECORE_EXE_EVENT_DATA = 0;
EAPI int ECORE_EXE_EVENT_ERROR = 0;

Eina_List *_ecore_exe_exes = NULL;

EAPI void
ecore_exe_run_priority_set(int pri)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   _impl_ecore_exe_run_priority_set(pri);
}

EAPI int
ecore_exe_run_priority_get(void)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(0);
   return _impl_ecore_exe_run_priority_get();
}

EAPI Ecore_Exe *
ecore_exe_run(const char *exe_cmd,
              const void *data)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   return ecore_exe_pipe_run(exe_cmd, 0, data);
}

EAPI Ecore_Exe *
ecore_exe_pipe_run(const char     *exe_cmd,
                   Ecore_Exe_Flags flags,
                   const void     *data)
{
   Ecore_Exe *ret = eo_add(MY_CLASS, NULL, ecore_obj_exe_command_set(eoid, exe_cmd, flags));
   if (ret)
     {
        Ecore_Exe_Data *pd = eo_data_scope_get(ret, MY_CLASS);
        pd->data = (void *) data;
     }

   return ret;
}

EOLIAN static void
_ecore_exe_command_set(Eo *obj EINA_UNUSED, Ecore_Exe_Data *pd, const char *cmd, Ecore_Exe_Flags flags)
{
   pd->cmd = strdup(cmd);
   pd->flags = flags;
}

EOLIAN static void
_ecore_exe_command_get(Eo *obj EINA_UNUSED, Ecore_Exe_Data *pd, const char **cmd, Ecore_Exe_Flags *flags)
{
   if (cmd) *cmd = pd->cmd;
   if (flags) *flags = pd->flags;
}

EOLIAN static Eo *
_ecore_exe_eo_base_finalize(Eo *obj, Ecore_Exe_Data *exe)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   obj = eo_finalize(eo_super(obj, MY_CLASS));

   if (!obj)
      return obj;

   return _impl_ecore_exe_eo_base_finalize(obj, exe);
}

EAPI void
ecore_exe_callback_pre_free_set(Ecore_Exe   *obj,
                                Ecore_Exe_Cb func)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return;
   exe->pre_free_cb = func;
}

EAPI Eina_Bool
ecore_exe_send(Ecore_Exe  *obj,
               const void *data,
               int         size)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(EINA_FALSE);
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return EINA_FALSE;

   if (exe->close_stdin)
   {
      ERR("Ecore_Exe %p stdin is closed! Cannot send %d bytes from %p",
          exe, size, data);
      return EINA_FALSE;
   }

   return _impl_ecore_exe_send(obj, exe, data, size);
}

EAPI void
ecore_exe_close_stdin(Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return;

   exe->close_stdin = 1;
}

EAPI void
ecore_exe_auto_limits_set(Ecore_Exe *obj,
                          int        start_bytes,
                          int        end_bytes,
                          int        start_lines,
                          int        end_lines)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return;

   _impl_ecore_exe_auto_limits_set(obj, exe, start_bytes, end_bytes, start_lines, end_lines);
}

EAPI Ecore_Exe_Event_Data *
ecore_exe_event_data_get(Ecore_Exe      *obj,
                         Ecore_Exe_Flags flags)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return NULL;

   return _impl_ecore_exe_event_data_get(obj, exe, flags);
}

EAPI void
ecore_exe_tag_set(Ecore_Exe  *obj,
                  const char *tag)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return;

   IF_FREE(exe->tag);
   if (tag)
     exe->tag = strdup(tag);
   else
     exe->tag = NULL;
}

EAPI const char *
ecore_exe_tag_get(const Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return NULL;

   return exe->tag;
}

EAPI void *
ecore_exe_free(Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return NULL;

   void *data = exe->data;
   eo_del(obj);

   return data;
}

EOLIAN static void
_ecore_exe_eo_base_destructor(Eo *obj, Ecore_Exe_Data *exe)
{
   eo_destructor(eo_super(obj, ECORE_EXE_CLASS));

   _impl_ecore_exe_eo_base_destructor(obj, exe);
}

EAPI void
ecore_exe_event_data_free(Ecore_Exe_Event_Data *e)
{
   if (!e) return;
   IF_FREE(e->lines);
   IF_FREE(e->data);
   free(e);
}

EAPI pid_t
ecore_exe_pid_get(const Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(0);
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return -1;

   return exe->pid;
}

EAPI const char *
ecore_exe_cmd_get(const Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   const char *ret = NULL;

   ecore_obj_exe_command_get(obj, &ret, NULL);

   return ret;
}

EAPI void *
ecore_exe_data_get(const Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return NULL;

   return exe->data;
}

EAPI void *
ecore_exe_data_set(Ecore_Exe *obj,
                   void      *data)
{
   void *ret;
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return NULL;

   ret = exe->data;
   exe->data = data;
   return ret;
}

EAPI Ecore_Exe_Flags
ecore_exe_flags_get(const Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(0);
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return 0;

   return exe->flags;
}

EAPI void
ecore_exe_pause(Ecore_Exe *obj)
{
   efl_control_suspend_set(obj, EINA_TRUE);
}

EAPI void
ecore_exe_continue(Ecore_Exe *obj)
{
   efl_control_suspend_set(obj, EINA_FALSE);
}

EOLIAN static void
_ecore_exe_efl_control_suspend_set(Eo *obj EINA_UNUSED, Ecore_Exe_Data *exe, Eina_Bool suspend)
{
   EINA_MAIN_LOOP_CHECK_RETURN;

   if (suspend)
     {
        _impl_ecore_exe_pause(obj, exe);
     }
   else
     {
        _impl_ecore_exe_continue(obj, exe);
     }
}

EAPI void
ecore_exe_interrupt(Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return;

   _impl_ecore_exe_interrupt(obj, exe);
}

EAPI void
ecore_exe_quit(Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return;

   _impl_ecore_exe_quit(obj, exe);
}

EAPI void
ecore_exe_terminate(Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return;

   _impl_ecore_exe_terminate(obj, exe);
}

EAPI void
ecore_exe_kill(Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return;

   _impl_ecore_exe_kill(obj, exe);
}

EAPI void
ecore_exe_signal(Ecore_Exe *obj,
                 int        num)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return;

   _impl_ecore_exe_signal(obj, exe, num);
}

EAPI void
ecore_exe_hup(Ecore_Exe *obj)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
   if (!eo_isa(obj, MY_CLASS))
      return;

   _impl_ecore_exe_hup(obj, exe);
}

void
_ecore_exe_init(void)
{
   ECORE_EXE_EVENT_ADD = ecore_event_type_new();
   ECORE_EXE_EVENT_DEL = ecore_event_type_new();
   ECORE_EXE_EVENT_DATA = ecore_event_type_new();
   ECORE_EXE_EVENT_ERROR = ecore_event_type_new();
}

void
_ecore_exe_shutdown(void)
{
   Ecore_Exe *exe = NULL;
   Eina_List *l1, *l2;
   EINA_LIST_FOREACH_SAFE(_ecore_exe_exes, l1, l2, exe)
      ecore_exe_free(exe);
}

Ecore_Exe *
_ecore_exe_find(pid_t pid)
{
   Eina_List *itr;
   Ecore_Exe *obj;

   EINA_LIST_FOREACH(_ecore_exe_exes, itr, obj)
   {
      Ecore_Exe_Data *exe = eo_data_scope_get(obj, MY_CLASS);
      if (exe->pid == pid)
        return obj;
   }
   return NULL;
}

void *
_ecore_exe_event_del_new(void)
{
   Ecore_Exe_Event_Del *e;

   e = calloc(1, sizeof(Ecore_Exe_Event_Del));
   return e;
}

void
_ecore_exe_event_del_free(void *data EINA_UNUSED,
                          void *ev)
{
   Ecore_Exe_Event_Del *e;

   e = ev;
   if (e->exe)
     ecore_exe_free(e->exe);
   free(e);
}

void
_ecore_exe_event_exe_data_free(void *data EINA_UNUSED,
                               void *ev)
{
   Ecore_Exe_Event_Data *e;

   e = ev;
   ecore_exe_event_data_free(e);
}

Ecore_Exe_Event_Add *
_ecore_exe_event_add_new(void)
{
   Ecore_Exe_Event_Add *e;

   e = calloc(1, sizeof(Ecore_Exe_Event_Add));
   return e;
}

void
_ecore_exe_event_add_free(void *data EINA_UNUSED,
                          void *ev)
{
   Ecore_Exe_Event_Add *e;

   e = ev;
   free(e);
}

#include "ecore_exe.eo.c"