summaryrefslogtreecommitdiff
path: root/src/bin/eolian/main.c
blob: 08d0bae9893032ab80e174bc52dc718e577ed168 (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
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
#include <getopt.h>

#include <Eina.h>
#include <Ecore_File.h>

#include "Eolian.h"
#include "legacy_generator.h"
#include "eo1_generator.h"

#define EO_SUFFIX ".eo"

static int eo_version = 0;

static Eina_Bool
_generate_h_file(char *filename, char *classname, Eina_Bool append)
{
   Eina_Bool ret = EINA_FALSE;
   Eina_Strbuf *hfile = eina_strbuf_new();
   if (append)
     {
        Eina_File *fn = eina_file_open(filename, EINA_FALSE);
        if (!fn)
          {
            printf ("Cant open file \"%s\" for updating.\n", filename);
            goto end;
          }

        eina_strbuf_append(hfile, (char*)eina_file_map_all(fn, EINA_FILE_SEQUENTIAL));
        eina_file_close(fn);

        legacy_header_append(classname, eo_version, hfile);
     }
   else
     {
        eo1_header_generate(classname, hfile);
     }
   const char *htext = eina_strbuf_string_get(hfile);

   FILE* fd = fopen(filename, "w");
   if (!fd)
     {
        printf ("Couldn't open file %s for writing\n", filename);
        goto end;
     }
   if (htext) fputs(htext, fd);
   fclose(fd);

   ret = EINA_TRUE;
end:
   eina_strbuf_free(hfile);

   return ret;
}

static Eina_Bool
_generate_c_file(char *filename, char *classname, Eina_Bool append)
{
   Eina_Bool ret = EINA_FALSE;

   Eina_Strbuf *cfile = eina_strbuf_new();
   legacy_source_generate(classname, eo_version, cfile);

   FILE* fd = fopen(filename, (append) ? "a" : "w");
   if (!fd)
     {
        printf ("Couldnt open file %s for writing\n", filename);
        goto end;
     }
   const char *ctext = eina_strbuf_string_get(cfile);
   if (ctext) fputs(ctext, fd);
   fclose(fd);

   ret = EINA_TRUE;
end:
   eina_strbuf_free(cfile);
   return ret;
}

// TODO join with header gen.
static Eina_Bool
_generate_legacy_header_file(char *filename, char *classname, Eina_Bool append)
{
   Eina_Bool ret = EINA_FALSE;

   Eina_Strbuf *lfile = eina_strbuf_new();

   if (append)
     {
        Eina_File *fn = eina_file_open(filename, EINA_FALSE);
        if (!fn)
          {
            printf ("Cant open file \"%s\" for updating.\n", filename);
            goto end;
          }
        eina_strbuf_append(lfile, (char*)eina_file_map_all(fn, EINA_FILE_SEQUENTIAL));
        eina_file_close(fn);

        legacy_header_append(classname, eo_version, lfile);
     }
   else
     {
        legacy_header_generate(classname, eo_version, lfile);
     }

   FILE* fd = fopen(filename, "w");
   if (!fd)
     {
        printf ("Couldnt open file %s for writing\n", filename);
        goto end;
     }
   const char *ltext = eina_strbuf_string_get(lfile);
   if (ltext) fputs(ltext, fd);
   fclose(fd);

   ret = EINA_TRUE;
end:
   eina_strbuf_free(lfile);
   return ret;
}

static Eina_Bool
_generate_eo_and_legacy_h_file(char *filename, char *classname)
{
   Eina_Bool ret = EINA_FALSE;

   Eina_Strbuf *hfile = eina_strbuf_new();

   FILE* fd = fopen(filename, "w");
   if (!fd)
     {
        printf ("Couldnt open file %s for writing\n", filename);
        goto end;
     }

   eo1_header_generate(classname, hfile);
   legacy_header_generate(classname, eo_version, hfile);
   const char *htext = eina_strbuf_string_get(hfile);
   if (htext) fputs(htext, fd);

   fclose(fd);

   ret = EINA_TRUE;
end:
   eina_strbuf_free(hfile);
   return ret;
}

int main(int argc, char **argv)
{
   eina_init();
   int ret = 0;
   Eina_Bool help = EINA_FALSE, show = EINA_FALSE;
   Eina_List *files = NULL, *itr;
   Eina_List *classes = NULL;
   char *h_filename = NULL, *c_filename = NULL,
        *classname = NULL, *leg_filename = NULL,
        *eoleg_filename = NULL;

   Eina_Bool happend = EINA_FALSE;
   Eina_Bool lappend = EINA_FALSE;

   static struct option long_options[] =
     {
        /* These options set a flag. */
          {"eo1",        no_argument,         &eo_version, 1},
          {"eo2",        no_argument,         &eo_version, 2},
          {"verbose",    no_argument,         0, 'V'},
          {"help",       no_argument,         0, 'h'},
          {"gh",         required_argument,   0, 1},
          {"gc",         required_argument,   0, 2},
          {"ah",         required_argument,   0, 3},
          {"al",         required_argument,   0, 4},
          {"gle",        required_argument,   0, 5},
          {"include",    required_argument,   0, 'I'},
          {"class",      required_argument,   0, 'c'},
          {0, 0, 0, 0}
     };
   int long_index =0, opt;
   while ((opt = getopt_long(argc, argv,"Vho:I:c:", long_options, &long_index )) != -1)
     {
        switch (opt) {
           case 0: break;
           case 1: h_filename = optarg; break;
           case 2: c_filename = optarg; break;
           case 3: h_filename = optarg; happend = EINA_TRUE; break;
           case 4: leg_filename = optarg; lappend = EINA_TRUE; break;
           case 5: eoleg_filename = optarg; break;
           case 'V': show = EINA_TRUE; break;
           case 'h': help = EINA_TRUE; break;
           case 'I':
                     {
                        printf("%s\n", optarg);
                        char *dir = ecore_file_realpath(optarg);
                        if (strlen(dir) != 0)
                          {
                             if (ecore_file_is_dir(dir))
                               {
                                  Eina_List *dir_files;
                                  char *file;
                                  /* Get all files from directory. Not recursively!!!*/
                                  dir_files = ecore_file_ls(dir);
                                  EINA_LIST_FREE(dir_files, file)
                                    {
                                       char *filepath = malloc(strlen(dir) + 1 + strlen(file) + 1);
                                       sprintf(filepath, "%s/%s", dir, file);
                                       if ((!ecore_file_is_dir(filepath)) && eina_str_has_suffix(filepath, EO_SUFFIX))
                                         {
                                            /* Allocated string will be freed during deletion of "files" list. */
                                            files = eina_list_append(files, strdup(filepath));
                                         }
                                       free(filepath);
                                       free(file);
                                    }
                               }
                          }
                        free(dir);
                        break;
                     }
           case 'c': classes = eina_list_append(classes, optarg); break;
           default: help = EINA_TRUE;
        }
     }

   if (eina_list_count(classes)) classname = eina_list_data_get(classes);

   if (!files || help || !classname)
     {
        printf("Usage: %s [-h/--help] [-V/--verbose] [-I/--include input_dir] [--gh|--gc|--ah] filename [-c/--class] classname \n", argv[0]);
        printf("       --eo1/--eo2 Set generator to eo1/eo2 mode. Must be specified\n");
        printf("       --gh Generate c header file [.h] for eo class specified by classname\n");
        printf("       --gc Generate c source file [.c] for eo class specified by classname\n");
        printf("       --ah Append eo class definitions to an existing c header file [.h]\n");
        printf("       --al Append legacy function definitions to an existing c header file [.h]\n");
        printf("       --gle Generate eo and legacy file [.h]\n");
        return 0;
     }

   eolian_init();
   const char *filename;
   EINA_LIST_FOREACH(files, itr, filename)
     {
        if (!eolian_eo_file_parse(filename))
          {
             printf("Error during parsing file %s\n", filename);
             goto end;
          }
     }

   if (show) eolian_show(classname);

   if (!eo_version)
     {
        printf("No eo version specified (use -eo1 or -eo2). Aborting eo generation.\n");
        ret = 1;
        goto end;
     }

   if (h_filename)
     {
        printf("%s header file %s\n", (happend) ? "Appending" : "Generating", h_filename);
        _generate_h_file(h_filename, classname, happend);
     }

   if (c_filename)
     {
        printf("Generating source file %s\n", c_filename);
        Eina_List *l = NULL;
        char *cname = NULL;
        EINA_LIST_FOREACH(classes,l,cname)
          _generate_c_file(c_filename, cname, (classes != l));
     }

   if (leg_filename)
     {
        printf("%s legacy file %s\n", (lappend) ? "Appending" : "Generating", leg_filename);
        _generate_legacy_header_file(leg_filename, classname, lappend);
     }

   if (eoleg_filename)
     {
        printf("Generating eo and legacy header file %s\n", eoleg_filename);
        _generate_eo_and_legacy_h_file(eoleg_filename, classname);
     }

end:
   EINA_LIST_FREE(files, filename)
      free((char *)filename);
   eina_list_free(classes);
   eolian_shutdown();
   eina_shutdown();
   return ret;
}