aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ecore/efl_core_proc_env.c
blob: 10ad1f98455c25f906a97d77d792037c1e3f1a5e (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Ecore.h>
#ifdef HAVE_CRT_EXTERNS_H
# include <crt_externs.h>
#endif
#include "ecore_private.h"

#define MY_CLASS EFL_CORE_PROC_ENV_CLASS

extern char **environ;

static Efl_Core_Env *env = NULL;

typedef struct {
   Eina_Bool in_sync;
} Efl_Core_Proc_Env_Data;

static void
_sync(Efl_Core_Env *obj, Efl_Core_Proc_Env_Data *pd)
{
   Eina_List *existing_keys = NULL, *n;
   Eina_Iterator *content;
   const char *key;

   pd->in_sync = EINA_TRUE;
   content = efl_core_env_content_get(obj);

   EINA_ITERATOR_FOREACH(content, key)
     {
        existing_keys = eina_list_append(existing_keys, key);
     }

   if (environ)
     {
        char **p;

        for (p = environ; *p; p++)
          {
             char **values;

             values = eina_str_split(*p, "=", 2);
             efl_core_env_set(obj, values[0], values[1]);

             EINA_LIST_FOREACH(existing_keys, n, key)
               {
                  if (!strcmp(key, values[0]))
                    {
                       existing_keys = eina_list_remove_list(existing_keys, n);
                       break;
                    }
               }
          }
     }
   EINA_LIST_FOREACH(existing_keys, n, key)
     {
        efl_core_env_unset(obj, key);
     }
   pd->in_sync = EINA_FALSE;
}

EOLIAN static const char*
_efl_core_proc_env_efl_core_env_env_get(const Eo *obj, Efl_Core_Proc_Env_Data *pd, const char *var)
{
   if (!pd->in_sync)
     _sync((Eo*)obj, pd);
   return efl_core_env_get(efl_super(obj, MY_CLASS), var);
}

EOLIAN static void
_efl_core_proc_env_efl_core_env_env_set(Eo *obj, Efl_Core_Proc_Env_Data *pd, const char *var, const char *value)
{
   efl_core_env_set(efl_super(obj, MY_CLASS), var, value);
   if (!pd->in_sync)
     {
        if (value)
          setenv(var, value, 1);
        else
          unsetenv(var);
     }
}

EOLIAN static void
_efl_core_proc_env_efl_core_env_unset(Eo *obj, Efl_Core_Proc_Env_Data *pd, const char *key)
{
   efl_core_env_unset(efl_super(obj, MY_CLASS), key);
   if (!pd->in_sync)
     {
        unsetenv(key);
     }
}

EOLIAN static void
_efl_core_proc_env_efl_core_env_clear(Eo *obj, Efl_Core_Proc_Env_Data *pd)
{
   efl_core_env_clear(efl_super(obj, MY_CLASS));
   if (!pd->in_sync)
     {
#ifdef HAVE_CLEARENV
        clearenv();
#else
        environ = NULL;
#endif
     }
}


EOLIAN static Efl_Duplicate*
_efl_core_proc_env_efl_duplicate_duplicate(const Eo *obj, Efl_Core_Proc_Env_Data *pd)
{
   if (!pd->in_sync)
     _sync((Eo*) obj, pd);
   return efl_duplicate(efl_super(obj, MY_CLASS));
}

EOLIAN static Eina_Iterator*
_efl_core_proc_env_efl_core_env_content_get(const Eo *obj, Efl_Core_Proc_Env_Data *pd)
{
   if (!pd->in_sync)
     _sync((Eo*) obj, pd);
   return efl_core_env_content_get(efl_super(obj, MY_CLASS));
}

EOLIAN static Efl_Object*
_efl_core_proc_env_efl_object_constructor(Eo *obj, Efl_Core_Proc_Env_Data *pd EINA_UNUSED)
{
   EINA_SAFETY_ON_TRUE_RETURN_VAL(!!env, NULL);

   obj = efl_constructor(efl_super(obj, MY_CLASS));
   return obj;
}

EOLIAN static Efl_Core_Env*
_efl_core_proc_env_self(void)
{
   if (!env)
     {
        efl_domain_current_push(EFL_ID_DOMAIN_SHARED);
        env = efl_add_ref(EFL_CORE_PROC_ENV_CLASS, NULL);
        efl_domain_current_pop();
        efl_wref_add(env, &env);
     }

   return env;
}

#include "efl_core_proc_env.eo.c"