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

#include <Ecore.h>

#include "ecore_private.h"

#define MY_CLASS EFL_TASK_CLASS

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////

static void
_clear_args(Efl_Task_Data *pd)
{
   unsigned int count, i;

   if (!pd->args) return;
   count = eina_array_count(pd->args);
   for (i = 0; i < count; i++)
     eina_stringshare_del(eina_array_data_get(pd->args, i));
   eina_array_free(pd->args);
   pd->args = NULL;
}

static Eina_Array *
_unescape(const char *s)
{
   Eina_Array *args;
   const char *p;
   char *tmp = NULL, *d = NULL;
   if (!s) return NULL;

   Eina_Bool in_quote_dbl = EINA_FALSE;
   Eina_Bool in_quote = EINA_FALSE;

   args = eina_array_new(16);
   if (!args) return NULL;
   for (p = s; *p; p++)
     {
        if (!tmp) tmp = d = strdup(p);
        if (tmp)
          {
             if (in_quote_dbl)
               {
                  switch (*p)
                    {
                     case '\"':
                       in_quote_dbl = EINA_FALSE;
                       *d = 0;
                       eina_array_push(args, eina_stringshare_add(tmp));
                       free(tmp);
                       tmp = d = NULL;
                       break;
                     case '\\':
                       p++;
                       EINA_FALLTHROUGH
                     default:
                       *d = *p;
                       d++;
                       break;
                    }
               }
             else if (in_quote)
               {
                  switch (*p)
                    {
                     case '\'':
                       in_quote = EINA_FALSE;
                       *d = 0;
                       eina_array_push(args, eina_stringshare_add(tmp));
                       free(tmp);
                       tmp = d = NULL;
                       break;
                     case '\\':
                       p++;
                       EINA_FALLTHROUGH
                     default:
                       *d = *p;
                       d++;
                       break;
                    }
               }
             else
               {
                  switch (*p)
                    {
                     case ' ':
                     case '\t':
                     case '\r':
                     case '\n':
                       *d = 0;
                       eina_array_push(args, eina_stringshare_add(tmp));
                       free(tmp);
                       tmp = d = NULL;
                       break;
                     case '\"':
                       in_quote_dbl = EINA_TRUE;
                       break;
                     case '\'':
                       in_quote = EINA_TRUE;
                       break;
                     case '\\':
                       p++;
                       EINA_FALLTHROUGH
                     default:
                       *d = *p;
                       d++;
                       break;
                    }
               }
          }
     }
   if (tmp)
     {
        *d = 0;
        eina_array_push(args, eina_stringshare_add(tmp));
        free(tmp);
     }
   return args;
}

static char *
_escape(const char *s)
{
   Eina_Bool need_quote = EINA_FALSE;
   const char *p;
   char *s2 = malloc((strlen(s) * 2) + 1 + 2), *d;

   if (!s2) return NULL;

   for (p = s; *p; p++)
     {
        switch (*p)
          {
           case '\'':
           case '\"':
           case '$':
           case '#':
           case ';':
           case '&':
           case '`':
           case '|':
           case '(':
           case ')':
           case '[':
           case ']':
           case '{':
           case '}':
           case '>':
           case '<':
           case '\n':
           case '\r':
           case '\t':
             need_quote = EINA_TRUE;
           default:
             break;
          }
     }

   d = s2;
   if (need_quote)
     {
        *d = '\"';
        d++;
     }
   for (p = s; *p; p++, d++)
     {
        switch (*p)
          {
           case ' ':
           case '\\':
           case '\'':
           case '\"':
             *d = '\\';
             d++;
             EINA_FALLTHROUGH
           default:
             *d = *p;
             break;
          }
     }
   if (need_quote)
     {
        *d = '\"';
        d++;
     }
   *d = 0;
   return s2;
}

static void
_rebuild_command(Efl_Task_Data *pd)
{
   unsigned int count, i;
   Eina_Strbuf *sb;
   const char *arg, *cmd;
   Eina_Bool have_args = EINA_FALSE;

   if (!pd->command_dirty) return;
   pd->command_dirty = EINA_FALSE;
   eina_stringshare_del(pd->command);
   pd->command = NULL;
   if (!pd->args) return;
   sb = eina_strbuf_new();
   if (!sb) return;
   count = eina_array_count(pd->args);
   for (i = 0; i < count; i++)
     {
        arg = eina_array_data_get(pd->args, i);
        if (arg)
          {
             char *str = _escape(arg);
             if (str)
               {
                  if (have_args) eina_strbuf_append(sb, " ");
                  eina_strbuf_append(sb, str);
                  free(str);
                  have_args = EINA_TRUE;
               }
          }
     }
   cmd = eina_strbuf_string_get(sb);
   if (cmd) pd->command = eina_stringshare_add(cmd);
   eina_strbuf_free(sb);
}


static Eina_Bool
_foreach_env_copy(const Eina_Hash *hash EINA_UNUSED, const void *key, void *data, void *fdata)
{
   if (data)
     {
        // only copy env vars not already set
        if (!eina_hash_find(fdata, key))
          eina_hash_add(fdata, key, eina_stringshare_add(data));
     }
   return EINA_TRUE;
}

//////////////////////////////////////////////////////////////////////////

EOLIAN static void
_efl_task_command_set(Eo *obj EINA_UNUSED, Efl_Task_Data *pd, const char *command)
{
   eina_stringshare_replace(&pd->command, command);
   _clear_args(pd);
   pd->args = _unescape(pd->command);
}

EOLIAN static const char *
_efl_task_command_get(Eo *obj EINA_UNUSED, Efl_Task_Data *pd)
{
   _rebuild_command(pd);
   return pd->command;
}

EOLIAN static unsigned int
_efl_task_arg_count_get(Eo *obj EINA_UNUSED, Efl_Task_Data *pd)
{
   if (!pd->args) return 0;
   return eina_array_count(pd->args);
}

EOLIAN static void
_efl_task_arg_value_set(Eo *obj EINA_UNUSED, Efl_Task_Data *pd, unsigned int num, const char *arg)
{
   const char *parg = NULL;
   unsigned int count;

   if (!pd->args) pd->args = eina_array_new(16);
   count = eina_array_count(pd->args);
   if ((count > 0) && (count > num))
     parg = eina_array_data_get(pd->args, num);
   else
     {
        unsigned int i;

        for (i = count; i <= num; i++)
          {
             eina_array_push(pd->args, "");
             eina_array_data_set(pd->args, i, NULL);
          }
     }

   if (arg)
     eina_array_data_set(pd->args, num, eina_stringshare_add(arg));
   else
     eina_array_data_set(pd->args, num, NULL);
   if (parg) eina_stringshare_del(parg);
   pd->command_dirty = EINA_TRUE;
}

EOLIAN static const char *
_efl_task_arg_value_get(Eo *obj EINA_UNUSED, Efl_Task_Data *pd, unsigned int num)
{
   unsigned int count;

   if (!pd->args) return NULL;
   count = eina_array_count(pd->args);
   if (num >= count) return NULL;
   return eina_array_data_get(pd->args, num);
}

EOLIAN static void
_efl_task_arg_append(Eo *obj EINA_UNUSED, Efl_Task_Data *pd, const char *arg)
{
   if (!pd->args) pd->args = eina_array_new(16);
   if (arg)
     eina_array_push(pd->args, eina_stringshare_add(arg));
   else
     eina_array_push(pd->args, NULL);
   pd->command_dirty = EINA_TRUE;
}

EOLIAN static void
_efl_task_arg_reset(Eo *obj EINA_UNUSED, Efl_Task_Data *pd)
{
   _clear_args(pd);
   pd->command_dirty = EINA_TRUE;
}

EOLIAN static void
_efl_task_env_set(Eo *obj EINA_UNUSED, Efl_Task_Data *pd, const char *var, const char *value)
{
   if (!var) return;
   if (!pd->env)
     pd->env = eina_hash_string_superfast_new
       ((Eina_Free_Cb)eina_stringshare_del);
   if (!pd->env) return;
   if ((value) && (*value))
     eina_hash_add(pd->env, var, eina_stringshare_add(value));
   else eina_hash_del(pd->env, var, NULL);
}


EOLIAN static const char *
_efl_task_env_get(Eo *obj EINA_UNUSED, Efl_Task_Data *pd, const char *var)
{
   if ((!var) || (!pd->env)) return NULL;
   return eina_hash_find(pd->env, var);
}

EOLIAN static void
_efl_task_env_reset(Eo *obj EINA_UNUSED, Efl_Task_Data *pd)
{
   if (pd->env) eina_hash_free(pd->env);
   pd->env = NULL;
}

EOLIAN static void
_efl_task_priority_set(Eo *obj EINA_UNUSED, Efl_Task_Data *pd, Efl_Task_Priority priority)
{
   pd->priority = priority;
}

EOLIAN static Efl_Task_Priority
_efl_task_priority_get(Eo *obj EINA_UNUSED, Efl_Task_Data *pd)
{
   return pd->priority;
}

EOLIAN static int
_efl_task_exit_code_get(Eo *obj EINA_UNUSED, Efl_Task_Data *pd)
{
   return pd->exit_code;
}

EOLIAN static void
_efl_task_flags_set(Eo *obj EINA_UNUSED, Efl_Task_Data *pd, Efl_Task_Flags flags)
{
   pd->flags = flags;
}

EOLIAN static Efl_Task_Flags
_efl_task_flags_get(Eo *obj EINA_UNUSED, Efl_Task_Data *pd)
{
   return pd->flags;
}

EOLIAN static Eina_Future *
_efl_task_run(Eo *obj EINA_UNUSED, Efl_Task_Data *pd EINA_UNUSED)
{
   // NOP
   return NULL;
}

EOLIAN static void
_efl_task_end(Eo *obj EINA_UNUSED, Efl_Task_Data *pd EINA_UNUSED)
{
   // NOP
}

EOLIAN static Efl_Object *
_efl_task_efl_object_constructor(Eo *obj, Efl_Task_Data *pd EINA_UNUSED)
{
   obj = efl_constructor(efl_super(obj, MY_CLASS));
   return obj;
}

EOLIAN static void
_efl_task_efl_object_destructor(Eo *obj EINA_UNUSED, Efl_Task_Data *pd)
{
   eina_stringshare_del(pd->command);
   pd->command = NULL;
   _clear_args(pd);
   if (pd->env) eina_hash_free(pd->env);
   pd->env = NULL;
   efl_destructor(efl_super(obj, MY_CLASS));
}

EOLIAN static void
_efl_task_efl_object_parent_set(Eo *obj, Efl_Task_Data *pd, Efl_Object *parent)
{
   Eo *loop;

   efl_parent_set(efl_super(obj, MY_CLASS), parent);
   // copy loop env into exe task env, if not already set in env (overridden)
   loop = efl_provider_find(parent, EFL_LOOP_CLASS);
   if (loop)
     {
        Efl_Task_Data *tdl = efl_data_scope_get(loop, EFL_TASK_CLASS);

        if (tdl)
          {
             if (!pd->env)
               pd->env = eina_hash_string_superfast_new
                 ((Eina_Free_Cb)eina_stringshare_del);
             if (tdl->env)
               eina_hash_foreach(tdl->env, _foreach_env_copy, pd->env);
          }
     }
}

//////////////////////////////////////////////////////////////////////////

#include "efl_task.eo.c"