summaryrefslogtreecommitdiff
path: root/xdt-csource/main.c
blob: d12f776a2232181ab12020e19142c99c8d2c3f33 (plain)
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
429
430
431
/*
 * Copyright (c) 2002-2015
 *       The Xfce development team. All rights reserved.
 *
 * Copyright (c) 2005-2007 Benedikt Meurer <benny@xfce.org>
 * Copyright (c) 2007      Nick Schermer <nick@xfce.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_LIBINTL_H
#include <libintl.h>
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <glib.h>
#include <glib/gprintf.h>
#include <glib/gstdio.h>



/* --- prototypes --- */
static void parse_args    (gint        *argc_p,
                           gchar     ***argv_p);
static void print_csource (FILE        *fp,
                           const gchar *data,
                           gsize        length,
                           const gchar *filename);
static void print_usage   (void);
static void print_version (void);



/* --- variables --- */
static gboolean     gen_buildlist = FALSE;
static gboolean     gen_stripcomments = FALSE;
static gboolean     gen_stripcontent = FALSE;
static const gchar *gen_linkage = "static ";
static const gchar *gen_varname = "my_data";
static const gchar *out_name = NULL;



static void
parse_args (gint    *argc_p,
            gchar ***argv_p)
{
  gchar **argv = *argv_p;
  gchar  *s;
  gint    argc = *argc_p;
  gint    n;
  gint    m;

  for (n = 1; n < argc; ++n)
    {
      if (strcmp (argv[n], "--help") == 0
          || strcmp (argv[n], "-h") == 0)
        {
          print_usage ();
          exit (EXIT_SUCCESS);
        }
      else if (strcmp (argv[n], "--version") == 0
          || strcmp (argv[n], "-V") == 0)
        {
          print_version ();
          exit (EXIT_SUCCESS);
        }
      else if (strcmp (argv[n], "--extern") == 0)
        {
          gen_linkage = "";
          argv[n] = NULL;
        }
      else if (strcmp (argv[n], "--static") == 0)
        {
          gen_linkage = "static ";
          argv[n] = NULL;
        }
      else if (strncmp (argv[n], "--name=", 7) == 0
          || strcmp (argv[n], "--name") == 0)
        {
          s = argv[n] + 6;

          if (G_LIKELY (*s == '='))
            {
              gen_varname = g_strdup (s + 1);
            }
          else if (n + 1 < argc)
            {
              gen_varname = g_strdup (argv[n + 1]);
              argv[n++] = NULL;
            }

          argv[n] = NULL;
        }
      else if (strcmp (argv[n], "--build-list") == 0)
        {
          gen_buildlist = TRUE;
          argv[n] = NULL;
        }
      else if (strcmp (argv[n], "--strip-comments") == 0)
        {
          gen_stripcomments = TRUE;
          argv[n] = NULL;
        }
      else if (strcmp (argv[n], "--strip-content") == 0)
        {
          gen_stripcontent = TRUE;
          argv[n] = NULL;
        }
      else if (strcmp (argv[n], "--output") == 0
          || strncmp (argv[n], "--output=", 9) == 0)
        {
          s = argv[n] + 8;
          if (G_LIKELY (*s == '='))
            {
              out_name = g_strdup (s + 1);
            }
          else if (n + 1 < argc)
            {
              out_name = g_strdup (argv[n + 1]);
              argv[n++] = NULL;
            }
          argv[n] = NULL;
        }
    }

  for (m = 0, n = 1; n < argc; ++n)
    {
      if (m > 0)
        {
          if (argv[n] != NULL)
            {
              argv[m++] = argv[n];
              argv[n] = NULL;
            }
        }
      else if (argv[n] == NULL)
        {
          m = n;
        }
    }

  if (m > 0)
    *argc_p = m;
}



static void
print_csource (FILE        *fp,
               const gchar *data,
               gsize        length,
               const gchar *filename)
{
  const guint8 *p = (const guint8 *) data;
  gboolean      pad = FALSE;
  gboolean      inside_comment = FALSE;
  gboolean      inside_content = TRUE;
  gint          column = 0;
  guint         real_length = 0;

  g_fprintf (fp, "/* automatically generated from %s */\n", filename);
  g_fprintf (fp, "#ifdef __SUNPRO_C\n");
  g_fprintf (fp, "#pragma align 4 (%s)\n", gen_varname);
  g_fprintf (fp, "#endif\n");
  g_fprintf (fp, "#ifdef __GNUC__\n");
  g_fprintf (fp, "%sconst char %s[] __attribute__ ((__aligned__ (4))) =\n", gen_linkage, gen_varname);
  g_fprintf (fp, "#else\n");
  g_fprintf (fp, "%sconst char %s[] =\n", gen_linkage, gen_varname);
  g_fprintf (fp, "#endif\n");
  g_fprintf (fp, "{\n");
  g_fprintf (fp, "  \"");

  for (; length-- > 0; ++p)
    {
      if (column > 70)
        {
          g_fprintf (fp, "\"\n  \"");
          column = 0;
        }

      /* strip XML/HTML comments */
      if (gen_stripcomments)
        {
          /* skip comments */
          if (length >= 4 && p[0] == '<' && p[1] == '!' && p[2] == '-' && p[3] == '-')
            {
              inside_comment = TRUE;
              length -= 3;
              p += 3;
              continue;
            }
          else if (inside_comment)
            {
              /* check for end of comment */
              if (length >= 3 && p[0] == '-' && p[1] == '-' && p[2] == '>')
                {
                  inside_comment = FALSE;
                  length -= 2;
                  p += 2;
                }
              continue;
            }
        }

      /* strip XML content (the stuff between the nodes) */
      if (gen_stripcontent)
        {
          if (!inside_content && *p == '>')
            inside_content = TRUE;
          else if (inside_content && *p == '<')
            inside_content = FALSE;
          else if (inside_content && !g_ascii_isspace (*p))
            inside_content = FALSE;
          else if (inside_content)
            continue;
        }

      if (*p == '\"')
        {
          column += g_fprintf (fp, "\\\"");
          pad = FALSE;
        }
      else if (*p == '\'')
        {
          column += g_fprintf (fp, "\\\'");
          pad = FALSE;
        }
      else if (*p == '\\')
        {
          column += g_fprintf (fp, "\\\\");
          pad = FALSE;
        }
      else if (*p == '\n')
        {
          column += g_fprintf (fp, "\\n");
          pad = FALSE;
        }
      else if (*p == '\r')
        {
          column += g_fprintf (fp, "\\r");
          pad = FALSE;
        }
      else if (*p == '\t')
        {
          column += g_fprintf (fp, "\\t");
          pad = FALSE;
        }
      else if (g_ascii_isprint (*p))
        {
          if (pad && g_ascii_isdigit (*p))
            column += g_fprintf (fp, "\"\"");
          column += g_fprintf (fp, "%c", *p);
          pad = FALSE;
        }
      else
        {
          column += g_fprintf (fp, "\\%03o", (guint) *p);
          pad = TRUE;
        }

      real_length++;
    }

  g_fprintf (fp, "\"\n};\n\n");
  g_fprintf (fp, "%sconst unsigned %s_length = %uu;\n\n", gen_linkage, gen_varname, real_length);
}



static void
print_usage (void)
{
  g_print ("  xdt-csource is a small utility that generates C code containing arbitrary data,\n");
  g_print ("  useful for compiling texts or other data directly into programs.\n");
  g_print ("  It either takes as input one file name to generate code for, or, using the --build-list option,\n");
  g_print ("  a list of (name, file) pairs to generate code for a list of images into named variables.\n");
  g_print ("  This is the successor of exo-csource.\n\n");
  g_print ("Usage: %s [options] [file]\n", g_get_prgname ());
  g_print ("       %s [options] --build-list [[name file]...]\n", g_get_prgname ());
  g_print ("\n");
  g_print ("  -h, --help        Print this help message and exit\n");
  g_print ("  -V, --version     Print version information and exit\n");
  g_print ("  --extern          Generate extern symbols\n");
  g_print ("  --static          Generate static symbols\n");
  g_print ("  --name=identifier C macro/variable name\n");
  g_print ("  --build-list      Parse (name, file) pairs\n");
  g_print ("  --strip-comments  Remove comments from XML files\n");
  g_print ("  --strip-content   Remove node contents from XML files\n");
  g_print ("  --output=filename Write generated csource to specified file\n");
  g_print ("\n");
}



static void
print_version (void)
{
  g_print ("%s %s\n\n", G_LOG_DOMAIN, PACKAGE_VERSION);
  g_print ("Copyright (c) 2005-2019\n");
  g_print ("\tThe Xfce development team. All rights reserved.\n\n");
  g_print ("%s comes with ABSOLUTELY NO WARRANTY,\n"
           "You may redistribute copies of %s under the terms of\n"
           "the GNU General Public License which can be found in the\n"
           "%s source package.\n\n", g_get_prgname (), g_get_prgname (), PACKAGE_TARNAME);
  g_print ("Please report bugs to <%s>.", PACKAGE_BUGREPORT);
  g_print ("\n");
}



int
main (int argc, char **argv)
{
  gboolean toggle = FALSE;
  GError  *error = NULL;
  gchar  **p;
  gchar   *filename;
  gchar   *data;
  gsize    length;
  gint     n;
  FILE    *out_file;

  setlocale (LC_ALL, NULL);

  /* set program name */
  g_set_prgname (g_path_get_basename (argv[0]));

  /* parse command line arguments */
  parse_args (&argc, &argv);

  if (!gen_buildlist)
    {
      if (G_UNLIKELY (argc != 2))
        {
          print_usage ();
          return EXIT_FAILURE;
        }
      if (out_name == NULL)
          out_file = stdout;
      else
        {
          out_file = g_fopen (out_name, "w");
          if (out_file == NULL) {
            g_fprintf (stderr, "Failed to open output file \"%s\"\n", out_name);
            return EXIT_FAILURE;
          }
        }

#ifdef G_OS_WIN32
      filename = g_locale_to_utf8 (argv[1], -1, NULL, NULL, NULL);
#else
      filename = argv[1];
#endif

      if (!g_file_get_contents (filename, &data, &length, &error))
        {
          g_fprintf (stderr, "%s: Failed to load \"%s\": %s\n",
                     g_get_prgname (), filename, error->message);
          g_error_free (error);
          return EXIT_FAILURE;
        }

      print_csource (out_file, data, length, filename);

      if (out_file != NULL && out_file != stdout)
        fclose (out_file);

      g_free (data);
    }
  else
    {
      for (n = argc - 1, p = argv + 1; n-- > 0; ++p, toggle = !toggle)
        {
#ifdef G_OS_WIN32
          filename = g_locale_to_utf8 (*p, -1, NULL, NULL, NULL);
#else
          filename = *p;
#endif

          if (!toggle)
            {
              gen_varname = filename;
            }
          else
            {
              if (!g_file_get_contents (filename, &data, &length, &error))
                {
                  g_fprintf (stderr, "%s: Failed to load \"%s\": %s\n",
                             g_get_prgname (), filename, error->message);
                  g_error_free (error);
                  return EXIT_FAILURE;
                }

              print_csource (stdout, data, length, filename);

              g_free (data);
            }
        }
    }

  return EXIT_SUCCESS;
}