aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/elementary/elm_prefs_cc.c
blob: 786330ad603f527d291d7fba3ef92139d938cd89 (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
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <string.h>
#include <locale.h>
#include <limits.h>
#include <sys/stat.h>

#ifdef _WIN32
# include <evil_private.h> /* realpath */
#endif

#include "elm_prefs_cc.h"

int _elm_prefs_cc_log_dom = -1;
static void main_help(void);

Eina_Prefix *pfx = NULL;
char *file_in = NULL;
const char *tmp_dir = NULL;
char *file_out = NULL;

static const char *progname = NULL;

static void
_elm_prefs_cc_log_cb(const Eina_Log_Domain *d,
                     Eina_Log_Level level,
                     const char *file,
                     const char *fnc,
                     int _line,
                     const char *fmt,
                     EINA_UNUSED void *data,
                     va_list args)
{
   if ((d->name) && (d->namelen == sizeof("elm_prefs_cc") - 1) &&
       (memcmp(d->name, "elm_prefs_cc", sizeof("elm_prefs_cc") - 1) == 0))
     {
        const char *prefix;

        eina_log_console_color_set(stderr, eina_log_level_color_get(level));
        switch (level)
          {
           case EINA_LOG_LEVEL_CRITICAL:
             prefix = "Critical. ";
             break;

           case EINA_LOG_LEVEL_ERR:
             prefix = "Error. ";
             break;

           case EINA_LOG_LEVEL_WARN:
             prefix = "Warning. ";
             break;

           default:
             prefix = "";
          }
        fprintf(stderr, "%s: %s", progname, prefix);
        eina_log_console_color_set(stderr, EINA_COLOR_RESET);

        vfprintf(stderr, fmt, args);
        putc('\n', stderr);
     }
   else
     eina_log_print_cb_stderr(d, level, file, fnc, _line, fmt, NULL, args);
}

static void
main_help(void)
{
   printf
     ("Usage:\n"
      "\t%s [OPTIONS] input_file.epc [output_file.epb]\n"
      "\n"
      "Where OPTIONS is one or more of:\n"
      "\n"
      "-v                       Verbose output\n"
     , progname);
}

int
main(int argc, char **argv)
{
   int i;
   struct stat st;
#ifdef HAVE_REALPATH
   char rpath[PATH_MAX], rpath2[PATH_MAX];
#endif

   setlocale(LC_NUMERIC, "C");

   if (!eina_init())
     return -1;

   _elm_prefs_cc_log_dom = eina_log_domain_register
       ("elm_prefs_cc", ELM_PREFS_CC_DEFAULT_LOG_COLOR);
   if (_elm_prefs_cc_log_dom < 0)
     {
        EINA_LOG_ERR("Unable to create a log domain.");
        exit(-1);
     }
   if (!eina_log_domain_level_check(_elm_prefs_cc_log_dom, EINA_LOG_LEVEL_WARN))
     eina_log_domain_level_set("elm_prefs_cc", EINA_LOG_LEVEL_WARN);

   progname = ecore_file_file_get(argv[0]);
   eina_log_print_cb_set(_elm_prefs_cc_log_cb, NULL);

   tmp_dir = eina_environment_tmp_get();

   for (i = 1; i < argc; i++)
     {
        if (!strcmp(argv[i], "-h"))
          {
             main_help();
             exit(0);
          }
        else if (!strcmp(argv[i], "-v"))
          {
             eina_log_domain_level_set("elm_prefs_cc", EINA_LOG_LEVEL_INFO);
          }
        else if (!file_in)
          file_in = argv[i];
        else if (!file_out)
          file_out = argv[i];
     }

   if (!file_in)
     {
        ERR("no input file specified.");
        main_help();
        exit(-1);
     }

   pfx = eina_prefix_new(argv[0], /* argv[0] value (optional) */
                         main, /* an optional symbol to check path of */
                         "ELM_PREFS", /* env var prefix to use (XXX_PREFIX, XXX_BIN_DIR etc. */
                         "elementary", /* dir to add after "share" (PREFIX/share/DIRNAME) */
                         "themes/default.edj",    /* a magic file to check for in PREFIX/share/DIRNAME for success */
                         PACKAGE_BIN_DIR, /* package bin dir @ compile time */
                         PACKAGE_LIB_DIR, /* package lib dir @ compile time */
                         PACKAGE_DATA_DIR, /* package data dir @ compile time */
                         PACKAGE_DATA_DIR); /* if locale needed  use LOCALE_DIR */

   /* check whether file_in exists */
#ifdef HAVE_REALPATH
   if (!realpath(file_in, rpath) || stat(rpath, &st) || !S_ISREG(st.st_mode))
#else
   if (stat(file_in, &st) || !S_ISREG(st.st_mode))
#endif
     {
        ERR("file not found: %s.", file_in);
        main_help();
        exit(-1);
     }

   if (!file_out)
     {
        char *suffix;

        if ((suffix = strstr(file_in, ".epc")) && (suffix[4] == 0))
          {
             file_out = strdup(file_in);
             if (file_out)
               {
                  suffix = strstr(file_out, ".epc");
                  strcpy(suffix, ".epb");
               }
          }
     }
   if (!file_out)
     {
        ERR("no output file specified.");
        main_help();
        exit(-1);
     }

#ifdef HAVE_REALPATH
   if (realpath(file_out, rpath2) && !strcmp(rpath, rpath2))
#else
   if (!strcmp(file_in, file_out))
#endif
     {
        ERR("input file equals output file.");
        main_help();
        exit(-1);
     }

   elm_prefs_file = mem_alloc(SZ(Elm_Prefs_File));
   elm_prefs_file->compiler = strdup("elm_prefs_cc");

   compile();

   data_init();
   data_write();
   data_shutdown();

   eina_prefix_free(pfx);
   pfx = NULL;

   eina_log_domain_unregister(_elm_prefs_cc_log_dom);
   eina_shutdown();

   return 0;
}