Imported Upstream version 1.7.1
[platform/upstream/edje.git] / src / bin / edje_cc_sources.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include <string.h>
6 #include <ctype.h>
7 #include <limits.h>
8
9 #include "edje_cc.h"
10
11 static Eet_Data_Descriptor *_srcfile_edd = NULL;
12 static Eet_Data_Descriptor *_srcfile_list_edd = NULL;
13
14 static Eet_Data_Descriptor *_external_edd = NULL;
15 static Eet_Data_Descriptor *_external_list_edd = NULL;
16
17 static Eet_Data_Descriptor *_font_edd = NULL;
18 static Eet_Data_Descriptor *_font_list_edd = NULL;
19
20 static SrcFile_List srcfiles = {NULL};
21
22 void
23 source_edd(void)
24 {
25    Eet_Data_Descriptor_Class eddc;
26
27    eet_eina_stream_data_descriptor_class_set(&eddc, sizeof (eddc), "srcfile", sizeof (SrcFile));
28    _srcfile_edd = eet_data_descriptor_stream_new(&eddc);
29    EET_DATA_DESCRIPTOR_ADD_BASIC(_srcfile_edd, SrcFile, "name", name, EET_T_INLINED_STRING);
30    EET_DATA_DESCRIPTOR_ADD_BASIC(_srcfile_edd, SrcFile, "file", file, EET_T_INLINED_STRING);
31
32    eet_eina_stream_data_descriptor_class_set(&eddc, sizeof (eddc), "srcfile_list", sizeof (SrcFile_List));
33    _srcfile_list_edd = eet_data_descriptor_stream_new(&eddc);
34    EET_DATA_DESCRIPTOR_ADD_LIST(_srcfile_list_edd, SrcFile_List, "list", list, _srcfile_edd);
35
36    eet_eina_stream_data_descriptor_class_set(&eddc, sizeof (eddc), "external", sizeof (External));
37    _external_edd = eet_data_descriptor_stream_new(&eddc);
38    EET_DATA_DESCRIPTOR_ADD_BASIC(_external_edd, External, "name", name, EET_T_INLINED_STRING);
39
40    eet_eina_stream_data_descriptor_class_set(&eddc, sizeof (eddc), "external_list", sizeof (External_List));
41    _external_list_edd = eet_data_descriptor_stream_new(&eddc);
42    EET_DATA_DESCRIPTOR_ADD_LIST(_external_list_edd, External_List, "list", list, _external_edd);
43
44    eet_eina_stream_data_descriptor_class_set(&eddc, sizeof (eddc), "font", sizeof (Font));
45    _font_edd = eet_data_descriptor_stream_new(&eddc);
46    EET_DATA_DESCRIPTOR_ADD_BASIC(_font_edd, Font, "file", file, EET_T_INLINED_STRING);
47    EET_DATA_DESCRIPTOR_ADD_BASIC(_font_edd, Font, "name", name, EET_T_INLINED_STRING);
48
49    eet_eina_stream_data_descriptor_class_set(&eddc, sizeof (eddc), "font_list", sizeof (Font_List));
50    _font_list_edd = eet_data_descriptor_stream_new(&eddc);
51    EET_DATA_DESCRIPTOR_ADD_LIST(_font_list_edd, Font_List, "list", list, _font_edd);
52 }
53
54 static void source_fetch_file(const char *fil, const char *filname);
55
56 static void
57 source_fetch_file(const char *fil, const char *filname)
58 {
59    FILE *f;
60    char buf[16 * 1024], *dir = NULL;
61    long sz;
62    size_t tmp;
63    ssize_t dir_len = 0;
64    SrcFile *sf;
65
66    f = fopen(fil, "rb");
67    if (!f)
68      {
69         ERR("Cannot open file '%s'", fil);
70         exit(-1);
71      }
72
73    fseek(f, 0, SEEK_END);
74    sz = ftell(f);
75    fseek(f, 0, SEEK_SET);
76    sf = mem_alloc(SZ(SrcFile));
77    sf->name = mem_strdup(filname);
78    sf->file = mem_alloc(sz + 1);
79    if (sz > 0)
80      {
81         tmp = fread(sf->file, sz, 1, f);
82         if (tmp != 1)
83           {
84              ERR("file length for (%s) doesn't match!", filname);
85              exit(-1);
86           }
87      }
88
89    sf->file[sz] = '\0';
90    fseek(f, 0, SEEK_SET);
91    srcfiles.list = eina_list_append(srcfiles.list, sf);
92
93    while (fgets(buf, sizeof(buf), f))
94      {
95         char *p, *pp;
96         int got_hash = 0;
97         int forgetit = 0;
98         int haveinclude = 0;
99         char *file = NULL, *fname = NULL;
100
101         p = buf;
102         while ((!forgetit) && (*p))
103           {
104              if (!got_hash)
105                {
106                   if (!isspace(*p))
107                     {
108                        if (*p == '#')
109                          got_hash = 1;
110                        else
111                          forgetit = 1;
112                     }
113                   p++;
114                }
115
116              if (!haveinclude)
117                {
118                   if (!isspace(*p))
119                     {
120                        if (!strncmp(p, "include", 7))
121                          {
122                             haveinclude = 1;
123                             p += 7;
124                          }
125                        /* HACK! the logic above should be fixed so
126                         * preprocessor statements don't have to begin
127                         * in column 0.
128                         * otoh, edje_cc should print a warning in that case,
129                         * since according to the standard, preprocessor
130                         * statements need to be put in column 0.
131                         */
132                        else if (!strncmp(p, "#include", 8))
133                          {
134                             haveinclude = 1;
135                             p += 8;
136                          }
137                        else
138                          forgetit = 1;
139                     }
140                }
141              else
142                {
143                   if (!isspace(*p))
144                     {
145                        char end = '\0';
146
147                        if (*p == '"') end = '"';
148                        else if (*p == '<') end = '>';
149
150                        if (end)
151                          {
152                             pp = strchr(p + 1, end);
153                             if (!pp)
154                               forgetit = 1;
155                             else
156                               {
157                                  char *slash;
158                                  ssize_t l = 0;
159
160                                  /* get the directory of the current file
161                                   * if we haven't already done so
162                                   */
163                                  if ((!dir) && (strrchr(fil, '/')))
164                                    {
165                                       dir = mem_strdup(fil);
166                                       slash = strrchr(dir, '/');
167                                       *slash = '\0';
168                                       dir_len = strlen(dir);
169                                    }
170
171                                  l = pp - p + dir_len + 1;
172                                  file = mem_alloc(l);
173
174                                  if (!dir_len)
175                                    {
176                                       snprintf(file, l - 1, "%s", p + 1);
177                                       file[l - 2] = 0;
178                                    }
179                                  else
180                                    {
181                                       snprintf(file, l, "%s/%s", dir, p + 1);
182                                       file[l - 1] = 0;
183                                    }
184
185
186                                  fname = strdup(p + 1);
187                                  pp = strrchr(fname, end);
188                                  if (pp) *pp = 0;
189                                  forgetit = 1;
190                               }
191                          }
192                        else
193                          forgetit = 1;
194                     }
195                   else
196                     p++;
197                }
198
199              got_hash = 0;
200           }
201         if ((file) && (fname))
202           {
203              source_fetch_file(file, fname);
204              free(file);
205              free(fname);
206           }
207      }
208    free(dir);
209    fclose(f);
210 }
211
212 void
213 source_fetch(void)
214 {
215    char buf[PATH_MAX] = {0}, *ptr;
216
217    ptr = strrchr(file_in, '/');
218    if (ptr)
219      {
220         snprintf(buf, sizeof (buf), "%s", ptr + 1);
221      }
222
223    source_fetch_file(file_in, buf[0] ? buf : file_in);
224 }
225
226 int
227 source_append(Eet_File *ef)
228 {
229    return eet_data_write(ef, _srcfile_list_edd, "edje_sources", &srcfiles,
230                          compress_mode);
231 }
232
233 SrcFile_List *
234 source_load(Eet_File *ef)
235 {
236    SrcFile_List *s;
237
238    s = eet_data_read(ef, _srcfile_list_edd, "edje_sources");
239    return s;
240 }
241
242 int
243 source_fontmap_save(Eet_File *ef, Eina_List *font_list)
244 {
245    Font_List fl;
246
247    fl.list = font_list;
248    return eet_data_write(ef, _font_list_edd, "edje_source_fontmap", &fl,
249                          compress_mode);
250 }
251
252 Font_List *
253 source_fontmap_load(Eet_File *ef)
254 {
255    Font_List *fl;
256
257    fl = eet_data_read(ef, _font_list_edd, "edje_source_fontmap");
258    return fl;
259 }