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

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

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

#define EO_SUFFIX ".eo"

static int eo_version = 0;
static Eina_Bool legacy_support = EINA_FALSE;

static Eina_Bool
_generate_h_file(char *filename, const 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)
          {
            ERR ("Cant open file \"%s\" for updating.", filename);
            goto end;
          }

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

        if (!legacy_header_append(classname, eo_version, hfile))
          {
             ERR("Failed to generate header for %s", classname);
             goto end;
          }
     }
   else
     {
        if (!eo1_header_generate(classname, hfile))
          {
             ERR("Failed to generate header for %s", classname);
             goto end;
          }
     }
   const char *htext = eina_strbuf_string_get(hfile);

   FILE* fd = fopen(filename, "w");
   if (!fd)
     {
        ERR ("Couldn't open file %s for writing", 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, const char *classname, Eina_Bool append)
{
   Eina_Bool ret = EINA_FALSE;

   Eina_Strbuf *cfile = eina_strbuf_new();
   if (!legacy_source_generate(classname, legacy_support, eo_version, cfile))
     {
        ERR("Failed to generate source for %s", classname);
        goto end;
     }

   FILE* fd = fopen(filename, (append) ? "a" : "w");
   if (!fd)
     {
        ERR("Couldnt open file %s for writing", 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, const 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)
          {
            ERR ("Cant open file \"%s\" for updating.", filename);
            goto end;
          }
        eina_strbuf_append(lfile, (char*)eina_file_map_all(fn, EINA_FILE_SEQUENTIAL));
        eina_file_close(fn);

        if (!legacy_header_append(classname, eo_version, lfile))
          {
             ERR("Failed to generate header for %s", classname);
             goto end;
          }
     }
   else
     {
        if (!eo1_header_generate(classname, lfile))
          {
             ERR("Failed to generate header for %s", classname);
             goto end;
          }
     }

   FILE* fd = fopen(filename, "w");
   if (!fd)
     {
        ERR ("Couldnt open file %s for writing", 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, const char *classname)
{
   Eina_Bool ret = EINA_FALSE;

   Eina_Strbuf *hfile = eina_strbuf_new();

   if (!eo1_header_generate(classname, hfile))
     {
        ERR("Failed to generate header for %s", classname);
        goto end;
     }
   if (!legacy_header_generate(classname, eo_version, hfile))
     {
        ERR("Failed to generate header for %s", classname);
        goto end;
     }

   const char *htext = eina_strbuf_string_get(hfile);
   FILE* fd = fopen(filename, "w");
   if (!fd)
     {
        ERR ("Couldnt open file %s for writing", filename);
        goto end;
     }

   if (htext) fputs(htext, fd);

   fclose(fd);

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

enum
{
   NO_WAY_GEN,
   H_GEN,
   C_GEN,
   H_EO_APP,
   H_LEG_APP,
   H_LEG_EO_GEN
};
int gen_opt = NO_WAY_GEN;

int main(int argc, char **argv)
{
   int ret = 1;
   Eina_Bool help = EINA_FALSE, show = EINA_FALSE;
   Eina_List *included_files = NULL, *itr;
   Eina_List *files4gen = NULL;
   const char *classname;
   char *output_filename = NULL; /* if NULL, have to generate, otherwise use the name stored there */

   eina_init();
   eolian_init();

   const char *log_dom = "eolian_gen";
   _eolian_gen_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_GREEN);
   if (_eolian_gen_log_dom < 0)
     {
        EINA_LOG_ERR("Could not register log domain: %s", log_dom);
        goto end;
     }

   eina_log_timing(_eolian_gen_log_dom,
                   EINA_LOG_STATE_STOP,
                   EINA_LOG_STATE_INIT);

   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",         no_argument,         &gen_opt, H_GEN},
          {"gc",         no_argument,         &gen_opt, C_GEN},
          {"ah",         no_argument,         &gen_opt, H_EO_APP},
          {"al",         no_argument,         &gen_opt, H_LEG_APP},
          {"gle",        no_argument,         &gen_opt, H_LEG_EO_GEN},
          {"output",     required_argument,   0, 'o'},
          {"legacy",     no_argument,         (int *)&legacy_support, EINA_TRUE},
          {"include",    required_argument,   0, 'I'},
          {0, 0, 0, 0}
     };
   int long_index =0, opt;
   while ((opt = getopt_long(argc, argv,"vho:I:", long_options, &long_index )) != -1)
     {
        switch (opt) {
           case 0: break;
           case 'o':
                   {
                      output_filename = strdup(optarg);
                      break;
                   }
           case 'v': show = EINA_TRUE; break;
           case 'h': help = EINA_TRUE; break;
           case 'I':
                     {
                        const char *dir = optarg;
                        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 "included_files" list. */
                                       included_files = eina_list_append(included_files, strdup(filepath));
                                    }
                                  free(filepath);
                                  free(file);
                               }
                          }
                        break;
                     }
           default: help = EINA_TRUE;
        }
     }
   while (optind < argc)
      files4gen = eina_list_append(files4gen, argv[optind++]);

   if (help)
     {
        printf("Usage: %s [-h/--help] [-v/--verbose] [-I/--include input_dir] [--legacy] [--gh|--gc|--ah] [--output/-o outfile] file.eo ... \n", argv[0]);
        printf("       --help/-h Print that help\n");
        printf("       --include/-I Include 'input_dir' as directory to search .eo files into\n");
        printf("       --output/-o Force output filename to 'outfile'\n");
        printf("       --eo1/--eo2 Set generator to eo1/eo2 mode. Must be specified\n");
        printf("       --gh Generate c header file [.h]\n");
        printf("       --gc Generate c source file [.c]\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");
        printf("       --legacy Generate legacy\n");
        ret = 0;
        goto end;
     }

   if (!files4gen)
     {
        ERR("No input files specified.\nTerminating.\n");
        goto end;
     }

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

   EINA_LIST_FOREACH(files4gen, itr, filename)
     {
        if (!eolian_eo_file_parse(filename))
          {
             ERR("Error during parsing file %s\n", filename);
             goto end;
          }
     }

   if (show)
     {
        EINA_LIST_FOREACH(files4gen, itr, filename)
          {
             const char *cname = eolian_class_find_by_file(filename);
             if (cname) eolian_show(cname);
          }
     }

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

   classname = eolian_class_find_by_file(eina_list_data_get(files4gen));

   if (gen_opt)
     {
        if (!output_filename)
          {
             output_filename = malloc(strlen(eina_list_data_get(files4gen)) + 5);
             strcpy(output_filename, eina_list_data_get(files4gen));
             if (C_GEN == gen_opt) strcat(output_filename, ".c");
             else strcat(output_filename, ".h");
          }
        switch (gen_opt)
          {
           case H_GEN: case H_EO_APP:
                {
                   INF("%s header file %s\n", (gen_opt == H_EO_APP) ? "Appending" : "Generating", output_filename);
                   ret = (_generate_h_file(output_filename, classname, gen_opt == H_EO_APP)?0:1);
                   break;
                }
           case H_LEG_APP:
                {
                   INF("Appending legacy file %s\n", output_filename);
                   ret = _generate_legacy_header_file(output_filename, classname, EINA_TRUE)?0:1;
                   break;
                }
           case H_LEG_EO_GEN:
                {
                   INF("Generating eo and legacy header file %s\n", output_filename);
                   ret = _generate_eo_and_legacy_h_file(output_filename, classname)?0:1;
                   break;
                }
           case C_GEN:
                {
                   INF("Generating source file %s\n", output_filename);
                   ret = _generate_c_file(output_filename, classname, EINA_FALSE)?0:1;
                   break;
                }
           default:
              ERR("Bad generation option\n");
              break;
          }
        free(output_filename);
     }
   else ret = 0;

end:
   EINA_LIST_FREE(included_files, filename)
      free((char *)filename);
   eina_list_free(files4gen);

   eina_log_timing(_eolian_gen_log_dom,
         EINA_LOG_STATE_START,
         EINA_LOG_STATE_SHUTDOWN);
   eina_log_domain_unregister(_eolian_gen_log_dom);
   _eolian_gen_log_dom = -1;

   eolian_shutdown();
   eina_shutdown();
   return ret;
}