summaryrefslogtreecommitdiff
path: root/src/bin/eolian2/main.c
blob: 731f1b1f52ec3e68d2f0ecf310773e93d28debee (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
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <unistd.h>
#include <libgen.h>

#include "main.h"
#include "types.h"

int _eolian_gen_log_dom = -1;

enum
{
   GEN_H        = 1 << 0,
   GEN_H_LEGACY = 1 << 1,
   GEN_H_STUB   = 1 << 2,
   GEN_C        = 1 << 3,
   GEN_C_IMPL   = 1 << 4
};

static const char *_dexts[5] =
{
  ".eo.h", ".eo.legacy.h", ".eo.stub.h", ".eo.c", ".eo.c"
};

static int
_get_bit_pos(int flag)
{
   int pos = 0;
   for (; !(flag & 1); flag >>= 1)
     ++pos;
   return pos;
}

static void
_print_usage(const char *progn, FILE *outf)
{
   fprintf(outf, "Usage: %s [options] [input]\n", progn);
   fprintf(outf, "Options:\n"
                 "  -I inc        include path \"inc\"\n"
                 "  -g type       generate file of type \"type\"\n"
                 "  -o name       specify the base name for output\n"
                 "  -o type:name  specify a particular output filename\n"
                 "  -h            print this message and exit\n"
                 "  -v            print version and exit\n"
                 "\n"
                 "Available types:\n"
                 "  h: C header file (.eo.h)\n"
                 "  l: Legacy C header file (.eo.legacy.h)\n"
                 "  s: Stub C header file (.eo.stub.h)\n"
                 "  c: C source file (.eo.c)\n"
                 "  i: Implementation file (added into .eo.c)\n"
                 "\n"
                 "By default, the 'hcl' set is used.\n"
                 "Output filenames are determined from input .eo filename.\n");
}

static void
_print_version(FILE *outf)
{
   fprintf(outf, "Eolian C generator version: " PACKAGE_VERSION "\n");
}

static Eina_Bool
_try_set_out(char t, char **outs, const char *val)
{
   int pos = -1;
   switch (t)
     {
      case 'h':
        pos = _get_bit_pos(GEN_H);
        break;
      case 'l':
        pos = _get_bit_pos(GEN_H_LEGACY);
        break;
      case 's':
        pos = _get_bit_pos(GEN_H_STUB);
        break;
      case 'c':
        pos = _get_bit_pos(GEN_C);
        break;
      case 'i':
        pos = _get_bit_pos(GEN_C_IMPL);
        break;
     }
   if (pos < 0)
     return EINA_FALSE;
   if (outs[pos])
     free(outs[pos]);
   outs[pos] = strdup(val);
   return EINA_TRUE;
}

static void _fill_all_outs(char **outs, const char *val)
{
   size_t vlen = strlen(val);
   for (size_t i = 0; i < (sizeof(_dexts) / sizeof(char *)); ++i)
   {
      if (outs[i])
        continue;
      size_t dlen = strlen(_dexts[i]);
      char *str = malloc(vlen + dlen + 1);
      memcpy(str, val, vlen);
      memcpy(str + vlen, _dexts[i], dlen);
      str[vlen + dlen] = '\0';
      outs[i] = str;
   }
}

static Eina_Strbuf *
_include_guard(const char *fname, const char *gname, Eina_Strbuf *buf)
{
   if (!buf || !eina_strbuf_length_get(buf))
     return buf;

   if (!gname)
     gname = "";

   char iname[256] = {0};
   strncpy(iname, fname, sizeof(iname) - 1);
   char *inamep = iname;
   eina_str_toupper(&inamep);

   Eina_Strbuf *g = eina_strbuf_new();
   eina_strbuf_append_printf(g, "#ifndef _%s_%s\n", iname, gname);
   eina_strbuf_append_printf(g, "#define _%s_%s\n\n", iname, gname);

   eina_strbuf_replace_all(g, ".", "_");
   eina_strbuf_append(g, eina_strbuf_string_get(buf));
   eina_strbuf_append(g, "\n#endif\n");
   eina_strbuf_free(buf);
   return g;
}

static Eina_Bool
_write_file(const char *fname, const Eina_Strbuf *buf, Eina_Bool append)
{
   FILE *f = fopen(fname, append ? "ab" : "wb");
   if (!f)
     {
        fprintf(stderr, "eolian: could not open '%s' (%s)\n",
                fname, strerror(errno));
        return EINA_FALSE;
     }

   Eina_Bool fret = EINA_TRUE;

   size_t bl = eina_strbuf_length_get(buf);
   if (!bl)
     goto end;

   if (fwrite(eina_strbuf_string_get(buf), 1, bl, f) != bl)
     {
        fprintf(stderr, "eolian: could not write '%s' (%s)\n",
                fname, strerror(errno));
        fret = EINA_FALSE;
     }

end:
   fclose(f);
   return fret;
}

static Eina_Strbuf *
_read_file(const char *fname)
{
   FILE *f = fopen(fname, "rb");
   if (!f)
     return NULL;

   fseek(f, 0, SEEK_END);
   long fs = ftell(f);
   if (fs < 0)
     {
        fprintf(stderr, "eolian: could not get length of '%s'\n", fname);
        fclose(f);
        return NULL;
     }
   fseek(f, 0, SEEK_SET);

   char *cont = malloc(fs + 1);
   if (!cont)
     {
        fprintf(stderr, "eolian: could not allocate memory for '%s'\n", fname);
        fclose(f);
        return NULL;
     }

   long as = fread(cont, 1, fs, f);
   if (as != fs)
     {
        fprintf(stderr, "eolian: could not read %ld bytes from '%s' (got %ld)\n",
                fs, fname, as);
        free(cont);
        fclose(f);
        return NULL;
     }

   cont[fs] = '\0';
   fclose(f);
   return eina_strbuf_manage_new_length(cont, fs);
}

char *eo_gen_c_full_name_get(const char *nm)
{
   if (!nm)
     return NULL;
   char *buf = strdup(nm);
   if (!buf)
     return NULL;
   for (char *p = strchr(buf, '.'); p; p = strchr(p, '.'))
     *p = '_';
   return buf;
}

static Eina_Bool
_write_header(const char *ofname, const char *ifname, Eina_Bool legacy)
{
   INF("generating header: %s (legacy: %d)", ofname, legacy);
   Eina_Strbuf *buf = eina_strbuf_new();

   eo_gen_types_header_gen(ifname, buf, EINA_TRUE, legacy);
   buf = _include_guard(ifname, "TYPES", buf);

   Eina_Strbuf *cltd = eo_gen_class_typedef_gen(ifname);
   if (cltd)
     {
        cltd = _include_guard(ifname, "CLASS_TYPE", cltd);
        eina_strbuf_prepend_char(buf, '\n');
        eina_strbuf_prepend(buf, eina_strbuf_string_get(cltd));
        eina_strbuf_free(cltd);
     }

   Eina_Bool ret = _write_file(ofname, buf, EINA_FALSE);
   eina_strbuf_free(buf);
   return ret;
}

static Eina_Bool
_write_stub_header(const char *ofname, const char *ifname)
{
   INF("generating stuv header: %s", ofname);
   Eina_Strbuf *buf = eina_strbuf_new();

   eo_gen_types_header_gen(ifname, buf, EINA_FALSE, EINA_FALSE);

   Eina_Strbuf *cltd = eo_gen_class_typedef_gen(ifname);
   if (cltd)
     {
        eina_strbuf_prepend_char(buf, '\n');
        eina_strbuf_prepend(buf, eina_strbuf_string_get(cltd));
        eina_strbuf_free(cltd);
     }

   buf = _include_guard(ifname, "STUBS", buf);

   Eina_Bool ret = _write_file(ofname, buf, EINA_FALSE);
   eina_strbuf_free(buf);
   return ret;
}

static Eina_Bool
_write_source(const char *ofname, const char *ifname)
{
   INF("generating source: %s", ofname);
   Eina_Strbuf *ebuf = eina_strbuf_new(),
               *lbuf = eina_strbuf_new();

   Eina_Bool ret = _write_file(ofname, ebuf, EINA_FALSE) &&
                   _write_file(ofname, lbuf, EINA_TRUE);
   eina_strbuf_free(ebuf);
   eina_strbuf_free(lbuf);
   return ret;
}

static Eina_Bool
_write_impl(const char *ofname, const char *ifname)
{
   INF("generating impl: %s", ofname);
   Eina_Strbuf *buf = eina_strbuf_new();

   Eina_Bool ret = _write_file(ofname, buf, EINA_FALSE);
   eina_strbuf_free(buf);
   return ret;
}

int
main(int argc, char **argv)
{
   int pret = 1;

   eina_init();
   eolian_init();

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

   eina_log_timing(_eolian_gen_log_dom, EINA_LOG_STATE_STOP, EINA_LOG_STATE_INIT);

   char *outs[5] = { NULL, NULL, NULL, NULL, NULL };

   int gen_what = 0;
   for (int opt; (opt = getopt(argc, argv, "I:g:o:hvV")) != -1;)
     switch (opt)
       {
        case 0:
          break;
        case 'I':
          if (!eolian_directory_scan(optarg))
            {
               fprintf(stderr, "eolian: could not scan '%s'\n", optarg);
               goto end;
            }
          break;
        case 'g':
          for (const char *wstr = optarg; *wstr; ++wstr)
            switch (*wstr)
              {
               case 'h':
                 gen_what |= GEN_H;
                 break;
               case 'l':
                 gen_what |= GEN_H_LEGACY;
                 break;
               case 's':
                 gen_what |= GEN_H_STUB;
                 break;
               case 'c':
                 gen_what |= GEN_C;
                 break;
               case 'i':
                 gen_what |= GEN_C_IMPL;
                 break;
               default:
                 fprintf(stderr, "unknown type: '%c'\n", *wstr);
                 goto end;
              }
        case 'o':
          if (strchr(optarg, ':'))
            {
               const char *abeg = optarg;
               const char *cpos = strchr(abeg, ':');
               if (((cpos - abeg) != 1) || !_try_set_out(*abeg, outs, cpos + 1))
                 {
                    char *oa = strdup(abeg);
                    oa[cpos - abeg] = '\0';
                    fprintf(stderr, "unknown type: '%s'\n", oa);
                    free(oa);
                    goto end;
                 }
            }
          else _fill_all_outs(outs, optarg);
          break;
        case 'h':
          _print_usage(argv[0], stdout);
          goto end;
        case 'v':
          _print_version(stdout);
          goto end;
        default:
          _print_usage(argv[0], stderr);
          goto end;
       }

   const char *input = argv[optind];
   if (!input)
     {
        fprintf(stderr, "eolian: no input file\n");
        goto end;
     }

   const char *ext = strrchr(input, '.');
   if (!ext || strcmp(ext, ".eo"))
     {
        fprintf(stderr, "eolian: invalid input file '%s'\n", input);
        goto end;
     }

   if (!eolian_file_parse(input))
     {
        fprintf(stderr, "eolian: could not parse file '%s'\n", input);
        goto end;
     }

   char *inoext = strdup(input);
   inoext[ext - input] = '\0';
   _fill_all_outs(outs, inoext);

   inoext[ext - input] = '.';
   char *eobn = basename(inoext);

   if (!gen_what)
     gen_what = GEN_H | GEN_H_LEGACY | GEN_C;

   Eina_Bool succ = EINA_TRUE;
   if (gen_what & GEN_H)
     succ = _write_header(outs[_get_bit_pos(GEN_H)], eobn, EINA_FALSE);
   if (succ && (gen_what & GEN_H_LEGACY))
     succ = _write_header(outs[_get_bit_pos(GEN_H_LEGACY)], eobn, EINA_TRUE);
   if (succ && (gen_what & GEN_H_STUB))
     succ = _write_stub_header(outs[_get_bit_pos(GEN_H_STUB)], eobn);
   if (succ && (gen_what & GEN_C))
     succ = _write_source(outs[_get_bit_pos(GEN_C)], eobn);
   if (succ && (gen_what & GEN_C_IMPL))
     succ = _write_impl(outs[_get_bit_pos(GEN_C_IMPL)], eobn);

   free(inoext);

   if (!succ)
     goto end;

   pret = 0;
end:
   eina_log_timing(_eolian_gen_log_dom, EINA_LOG_STATE_START, EINA_LOG_STATE_SHUTDOWN);
   eina_log_domain_unregister(_eolian_gen_log_dom);
   for (size_t i = 0; i < (sizeof(_dexts) / sizeof(char *)); ++i)
     free(outs[i]);
   eolian_shutdown();
   eina_shutdown();

   return pret;
}