Add comment about dup(0) and lack of /dev/stdin on Win32. (#407431)
[platform/upstream/glib.git] / gobject / glib-genmarshal.c
1 /* GLIB-GenMarshal - Marshaller generator for GObject library
2  * Copyright (C) 2000-2001 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include        "config.h"
20
21 #undef G_LOG_DOMAIN
22 #define G_LOG_DOMAIN "GLib-Genmarshal"
23 #include        <glib.h>
24
25 #include        <glib/gprintf.h>
26 #include        <stdlib.h>
27 #include        <fcntl.h>
28 #include        <string.h>
29 #include        <errno.h>
30 #ifdef HAVE_UNISTD_H
31 #include        <unistd.h>
32 #endif
33 #include        <sys/types.h>
34 #include        <sys/stat.h>
35
36 #ifdef G_OS_WIN32
37 #include <io.h>
38 #endif
39
40 /* --- defines --- */
41 #define PRG_NAME        "glib-genmarshal"
42 #define PKG_NAME        "GLib"
43 #define PKG_HTTP_HOME   "http://www.gtk.org"
44
45
46 /* --- typedefs & structures --- */
47 typedef struct
48 {
49   gchar       *keyword;         /* marhaller list keyword [MY_STRING] */
50   const gchar *sig_name;        /* signature name [STRING] */
51   const gchar *ctype;           /* C type name [gchar*] */
52   const gchar *getter;          /* value getter function [g_value_get_string] */
53 } InArgument;
54 typedef struct
55 {
56   gchar       *keyword;         /* marhaller list keyword [MY_STRING] */
57   const gchar *sig_name;        /* signature name [STRING] */
58   const gchar *ctype;           /* C type name [gchar*] */
59   const gchar *setter;          /* value setter function [g_value_set_string] */
60 } OutArgument;
61 typedef struct
62 {
63   gchar       *ploc;
64   OutArgument *rarg;
65   GList       *args;    /* of type InArgument* */
66 } Signature;
67
68
69 /* --- prototypes --- */
70 static void     parse_args      (gint           *argc_p,
71                                  gchar       ***argv_p);
72 static void     print_blurb     (FILE           *bout,
73                                  gboolean        print_help);
74
75
76 /* --- variables --- */
77 static const GScannerConfig scanner_config_template =
78 {
79   (
80    " \t\r"              /* "\n" is statement delimiter */
81    )                    /* cset_skip_characters */,
82   (
83    G_CSET_a_2_z
84    "_"
85    G_CSET_A_2_Z
86    )                    /* cset_identifier_first */,
87   (
88    G_CSET_a_2_z
89    "_0123456789"
90    G_CSET_A_2_Z
91    )                    /* cset_identifier_nth */,
92   ( "#\n" )             /* cpair_comment_single */,
93
94   FALSE                 /* case_sensitive */,
95
96   TRUE                  /* skip_comment_multi */,
97   TRUE                  /* skip_comment_single */,
98   TRUE                  /* scan_comment_multi */,
99   TRUE                  /* scan_identifier */,
100   FALSE                 /* scan_identifier_1char */,
101   FALSE                 /* scan_identifier_NULL */,
102   TRUE                  /* scan_symbols */,
103   FALSE                 /* scan_binary */,
104   TRUE                  /* scan_octal */,
105   TRUE                  /* scan_float */,
106   TRUE                  /* scan_hex */,
107   FALSE                 /* scan_hex_dollar */,
108   TRUE                  /* scan_string_sq */,
109   TRUE                  /* scan_string_dq */,
110   TRUE                  /* numbers_2_int */,
111   FALSE                 /* int_2_float */,
112   FALSE                 /* identifier_2_string */,
113   TRUE                  /* char_2_token */,
114   FALSE                 /* symbol_2_token */,
115   FALSE                 /* scope_0_fallback */,
116 };
117 static gchar            * const std_marshaller_prefix = "g_cclosure_marshal";
118 static gchar            *marshaller_prefix = "g_cclosure_user_marshal";
119 static GHashTable       *marshallers = NULL;
120 static FILE             *fout = NULL;
121 static gboolean          gen_cheader = FALSE;
122 static gboolean          gen_cbody = FALSE;
123 static gboolean          gen_internal = FALSE;
124 static gboolean          skip_ploc = FALSE;
125 static gboolean          std_includes = TRUE;
126
127
128 /* --- functions --- */
129 static void
130 put_marshal_value_getters (void)
131 {
132   fputs ("\n", fout);
133   fputs ("#ifdef G_ENABLE_DEBUG\n", fout);
134   fputs ("#define g_marshal_value_peek_boolean(v)  g_value_get_boolean (v)\n", fout);
135   fputs ("#define g_marshal_value_peek_char(v)     g_value_get_char (v)\n", fout);
136   fputs ("#define g_marshal_value_peek_uchar(v)    g_value_get_uchar (v)\n", fout);
137   fputs ("#define g_marshal_value_peek_int(v)      g_value_get_int (v)\n", fout);
138   fputs ("#define g_marshal_value_peek_uint(v)     g_value_get_uint (v)\n", fout);
139   fputs ("#define g_marshal_value_peek_long(v)     g_value_get_long (v)\n", fout);
140   fputs ("#define g_marshal_value_peek_ulong(v)    g_value_get_ulong (v)\n", fout);
141   fputs ("#define g_marshal_value_peek_int64(v)    g_value_get_int64 (v)\n", fout);
142   fputs ("#define g_marshal_value_peek_uint64(v)   g_value_get_uint64 (v)\n", fout);
143   fputs ("#define g_marshal_value_peek_enum(v)     g_value_get_enum (v)\n", fout);
144   fputs ("#define g_marshal_value_peek_flags(v)    g_value_get_flags (v)\n", fout);
145   fputs ("#define g_marshal_value_peek_float(v)    g_value_get_float (v)\n", fout);
146   fputs ("#define g_marshal_value_peek_double(v)   g_value_get_double (v)\n", fout);
147   fputs ("#define g_marshal_value_peek_string(v)   (char*) g_value_get_string (v)\n", fout);
148   fputs ("#define g_marshal_value_peek_param(v)    g_value_get_param (v)\n", fout);
149   fputs ("#define g_marshal_value_peek_boxed(v)    g_value_get_boxed (v)\n", fout);
150   fputs ("#define g_marshal_value_peek_pointer(v)  g_value_get_pointer (v)\n", fout);
151   fputs ("#define g_marshal_value_peek_object(v)   g_value_get_object (v)\n", fout);
152   fputs ("#else /* !G_ENABLE_DEBUG */\n", fout);
153   fputs ("/* WARNING: This code accesses GValues directly, which is UNSUPPORTED API.\n", fout);
154   fputs (" *          Do not access GValues directly in your code. Instead, use the\n", fout);
155   fputs (" *          g_value_get_*() functions\n", fout);
156   fputs (" */\n", fout);
157   fputs ("#define g_marshal_value_peek_boolean(v)  (v)->data[0].v_int\n", fout);
158   fputs ("#define g_marshal_value_peek_char(v)     (v)->data[0].v_int\n", fout);
159   fputs ("#define g_marshal_value_peek_uchar(v)    (v)->data[0].v_uint\n", fout);
160   fputs ("#define g_marshal_value_peek_int(v)      (v)->data[0].v_int\n", fout);
161   fputs ("#define g_marshal_value_peek_uint(v)     (v)->data[0].v_uint\n", fout);
162   fputs ("#define g_marshal_value_peek_long(v)     (v)->data[0].v_long\n", fout);
163   fputs ("#define g_marshal_value_peek_ulong(v)    (v)->data[0].v_ulong\n", fout);
164   fputs ("#define g_marshal_value_peek_int64(v)    (v)->data[0].v_int64\n", fout);
165   fputs ("#define g_marshal_value_peek_uint64(v)   (v)->data[0].v_uint64\n", fout);
166   fputs ("#define g_marshal_value_peek_enum(v)     (v)->data[0].v_long\n", fout);
167   fputs ("#define g_marshal_value_peek_flags(v)    (v)->data[0].v_ulong\n", fout);
168   fputs ("#define g_marshal_value_peek_float(v)    (v)->data[0].v_float\n", fout);
169   fputs ("#define g_marshal_value_peek_double(v)   (v)->data[0].v_double\n", fout);
170   fputs ("#define g_marshal_value_peek_string(v)   (v)->data[0].v_pointer\n", fout);
171   fputs ("#define g_marshal_value_peek_param(v)    (v)->data[0].v_pointer\n", fout);
172   fputs ("#define g_marshal_value_peek_boxed(v)    (v)->data[0].v_pointer\n", fout);
173   fputs ("#define g_marshal_value_peek_pointer(v)  (v)->data[0].v_pointer\n", fout);
174   fputs ("#define g_marshal_value_peek_object(v)   (v)->data[0].v_pointer\n", fout);
175   fputs ("#endif /* !G_ENABLE_DEBUG */\n", fout);
176   fputs ("\n", fout);
177 }
178
179 static gboolean
180 complete_in_arg (InArgument *iarg)
181 {
182   static const InArgument args[] = {
183     /* keyword          sig_name        ctype           getter                  */
184     { "VOID",           "VOID",         "void",         NULL,                   },
185     { "BOOLEAN",        "BOOLEAN",      "gboolean",     "g_marshal_value_peek_boolean", },
186     { "CHAR",           "CHAR",         "gchar",        "g_marshal_value_peek_char",    },
187     { "UCHAR",          "UCHAR",        "guchar",       "g_marshal_value_peek_uchar",   },
188     { "INT",            "INT",          "gint",         "g_marshal_value_peek_int",     },
189     { "UINT",           "UINT",         "guint",        "g_marshal_value_peek_uint",    },
190     { "LONG",           "LONG",         "glong",        "g_marshal_value_peek_long",    },
191     { "ULONG",          "ULONG",        "gulong",       "g_marshal_value_peek_ulong",   },
192     { "INT64",          "INT64",        "gint64",       "g_marshal_value_peek_int64",   },
193     { "UINT64",         "UINT64",       "guint64",      "g_marshal_value_peek_uint64",  },
194     { "ENUM",           "ENUM",         "gint",         "g_marshal_value_peek_enum",    },
195     { "FLAGS",          "FLAGS",        "guint",        "g_marshal_value_peek_flags",   },
196     { "FLOAT",          "FLOAT",        "gfloat",       "g_marshal_value_peek_float",   },
197     { "DOUBLE",         "DOUBLE",       "gdouble",      "g_marshal_value_peek_double",  },
198     { "STRING",         "STRING",       "gpointer",     "g_marshal_value_peek_string",  },
199     { "PARAM",          "PARAM",        "gpointer",     "g_marshal_value_peek_param",   },
200     { "BOXED",          "BOXED",        "gpointer",     "g_marshal_value_peek_boxed",   },
201     { "POINTER",        "POINTER",      "gpointer",     "g_marshal_value_peek_pointer", },
202     { "OBJECT",         "OBJECT",       "gpointer",     "g_marshal_value_peek_object",  },
203     /* deprecated: */
204     { "NONE",           "VOID",         "void",         NULL,                   },
205     { "BOOL",           "BOOLEAN",      "gboolean",     "g_marshal_value_peek_boolean", },
206   };
207   guint i;
208
209   g_return_val_if_fail (iarg != NULL, FALSE);
210
211   for (i = 0; i < G_N_ELEMENTS (args); i++)
212     if (strcmp (args[i].keyword, iarg->keyword) == 0)
213       {
214         iarg->sig_name = args[i].sig_name;
215         iarg->ctype = args[i].ctype;
216         iarg->getter = args[i].getter;
217
218         return TRUE;
219       }
220   return FALSE;
221 }
222
223 static gboolean
224 complete_out_arg (OutArgument *oarg)
225 {
226   static const OutArgument args[] = {
227     /* keyword          sig_name        ctype           setter                  */
228     { "VOID",           "VOID",         "void",         NULL,                                        },
229     { "BOOLEAN",        "BOOLEAN",      "gboolean",     "g_value_set_boolean",                       },
230     { "CHAR",           "CHAR",         "gchar",        "g_value_set_char",                          },
231     { "UCHAR",          "UCHAR",        "guchar",       "g_value_set_uchar",                         },
232     { "INT",            "INT",          "gint",         "g_value_set_int",                           },
233     { "UINT",           "UINT",         "guint",        "g_value_set_uint",                          },
234     { "LONG",           "LONG",         "glong",        "g_value_set_long",                          },
235     { "ULONG",          "ULONG",        "gulong",       "g_value_set_ulong",                         },
236     { "INT64",          "INT64",        "gint64",       "g_value_set_int64",                         },
237     { "UINT64",         "UINT64",       "guint64",      "g_value_set_uint64",                        },
238     { "ENUM",           "ENUM",         "gint",         "g_value_set_enum",                          },
239     { "FLAGS",          "FLAGS",        "guint",        "g_value_set_flags",                         },
240     { "FLOAT",          "FLOAT",        "gfloat",       "g_value_set_float",                         },
241     { "DOUBLE",         "DOUBLE",       "gdouble",      "g_value_set_double",                        },
242     { "STRING",         "STRING",       "gchar*",       "g_value_take_string",                       },
243     { "PARAM",          "PARAM",        "GParamSpec*",  "g_value_take_param",                        },
244     { "BOXED",          "BOXED",        "gpointer",     "g_value_take_boxed",                        },
245     { "POINTER",        "POINTER",      "gpointer",     "g_value_set_pointer",                       },
246     { "OBJECT",         "OBJECT",       "GObject*",     "g_value_take_object",                       },
247     /* deprecated: */
248     { "NONE",           "VOID",         "void",         NULL,                                        },
249     { "BOOL",           "BOOLEAN",      "gboolean",     "g_value_set_boolean",                       },
250   };
251   guint i;
252
253   g_return_val_if_fail (oarg != NULL, FALSE);
254
255   for (i = 0; i < G_N_ELEMENTS (args); i++)
256     if (strcmp (args[i].keyword, oarg->keyword) == 0)
257       {
258         oarg->sig_name = args[i].sig_name;
259         oarg->ctype = args[i].ctype;
260         oarg->setter = args[i].setter;
261
262         return TRUE;
263       }
264   return FALSE;
265 }
266
267 static const gchar*
268 pad (const gchar *string)
269 {
270 #define PAD_LENGTH      12
271   static gchar *buffer = NULL;
272   gint i;
273
274   g_return_val_if_fail (string != NULL, NULL);
275
276   if (!buffer)
277     buffer = g_new (gchar, PAD_LENGTH + 1);
278
279   /* paranoid check */
280   if (strlen (string) >= PAD_LENGTH)
281     {
282       g_free (buffer);
283       buffer = g_strdup_printf ("%s ", string);
284       g_warning ("overfull string (%u bytes) for padspace",
285                  (guint) strlen (string));
286
287       return buffer;
288     }
289
290   for (i = 0; i < PAD_LENGTH; i++)
291     {
292       gboolean done = *string == 0;
293
294       buffer[i] = done ? ' ' : *string++;
295     }
296   buffer[i] = 0;
297
298   return buffer;
299 }
300
301 static const gchar*
302 indent (guint n_spaces)
303 {
304   static gchar *buffer = NULL;
305   static guint blength = 0;
306
307   if (blength <= n_spaces)
308     {
309       blength = n_spaces + 1;
310       g_free (buffer);
311       buffer = g_new (gchar, blength);
312     }
313   memset (buffer, ' ', n_spaces);
314   buffer[n_spaces] = 0;
315
316   return buffer;
317 }
318
319 static void
320 generate_marshal (const gchar *signame,
321                   Signature   *sig)
322 {
323   guint ind, a;
324   GList *node;
325   gchar *tmp = g_strconcat (marshaller_prefix, "_", signame, NULL);
326   gboolean have_std_marshaller = FALSE;
327
328   /* here we have to make sure a marshaller named <marshaller_prefix>_<signame>
329    * exists. we might have put it out already, can revert to a standard
330    * marshaller provided by glib, or need to generate one.
331    */
332
333   if (g_hash_table_lookup (marshallers, tmp))
334     {
335       /* done, marshaller already generated */
336       g_free (tmp);
337       return;
338     }
339   else
340     {
341       /* need to alias/generate marshaller, register name */
342       g_hash_table_insert (marshallers, tmp, tmp);
343     }
344
345   /* can we revert to a standard marshaller? */
346   if (std_includes)
347     {
348       tmp = g_strconcat (std_marshaller_prefix, "_", signame, NULL);
349       have_std_marshaller = g_hash_table_lookup (marshallers, tmp) != NULL;
350       g_free (tmp);
351     }
352
353   if (gen_cheader && have_std_marshaller)
354     {
355       g_fprintf (fout, "#define %s_%s\t%s_%s\n", marshaller_prefix, signame, std_marshaller_prefix, signame);
356     }
357   if (gen_cheader && !have_std_marshaller)
358     {
359       ind = g_fprintf (fout, gen_internal ? "G_GNUC_INTERNAL " : "extern ");
360       ind += g_fprintf (fout, "void ");
361       ind += g_fprintf (fout, "%s_%s (", marshaller_prefix, signame);
362       g_fprintf (fout,   "GClosure     *closure,\n");
363       g_fprintf (fout, "%sGValue       *return_value,\n", indent (ind));
364       g_fprintf (fout, "%sguint         n_param_values,\n", indent (ind));
365       g_fprintf (fout, "%sconst GValue *param_values,\n", indent (ind));
366       g_fprintf (fout, "%sgpointer      invocation_hint,\n", indent (ind));
367       g_fprintf (fout, "%sgpointer      marshal_data);\n",
368                  indent (ind));
369     }
370   if (gen_cbody && !have_std_marshaller)
371     {
372       /* cfile marshal header */
373       g_fprintf (fout, "void\n");
374       ind = g_fprintf (fout, "%s_%s (", marshaller_prefix, signame);
375       g_fprintf (fout,   "GClosure     *closure,\n");
376       g_fprintf (fout, "%sGValue       *return_value,\n", indent (ind));
377       g_fprintf (fout, "%sguint         n_param_values,\n", indent (ind));
378       g_fprintf (fout, "%sconst GValue *param_values,\n", indent (ind));
379       g_fprintf (fout, "%sgpointer      invocation_hint,\n", indent (ind));
380       g_fprintf (fout, "%sgpointer      marshal_data)\n", indent (ind));
381       g_fprintf (fout, "{\n");
382
383       /* cfile GMarshalFunc typedef */
384       ind = g_fprintf (fout, "  typedef %s (*GMarshalFunc_%s) (", sig->rarg->ctype, signame);
385       g_fprintf (fout, "%s data1,\n", pad ("gpointer"));
386       for (a = 1, node = sig->args; node; node = node->next)
387         {
388           InArgument *iarg = node->data;
389
390           if (iarg->getter)
391             g_fprintf (fout, "%s%s arg_%d,\n", indent (ind), pad (iarg->ctype), a++);
392         }
393       g_fprintf (fout, "%s%s data2);\n", indent (ind), pad ("gpointer"));
394
395       /* cfile marshal variables */
396       g_fprintf (fout, "  register GMarshalFunc_%s callback;\n", signame);
397       g_fprintf (fout, "  register GCClosure *cc = (GCClosure*) closure;\n");
398       g_fprintf (fout, "  register gpointer data1, data2;\n");
399       if (sig->rarg->setter)
400         g_fprintf (fout, "  %s v_return;\n", sig->rarg->ctype);
401
402       if (sig->args || sig->rarg->setter)
403         {
404           g_fprintf (fout, "\n");
405
406           if (sig->rarg->setter)
407             g_fprintf (fout, "  g_return_if_fail (return_value != NULL);\n");
408           if (sig->args)
409             {
410               for (a = 0, node = sig->args; node; node = node->next)
411                 {
412                   InArgument *iarg = node->data;
413
414                   if (iarg->getter)
415                     a++;
416                 }
417               g_fprintf (fout, "  g_return_if_fail (n_param_values == %u);\n", 1 + a);
418             }
419         }
420
421       /* cfile marshal data1, data2 and callback setup */
422       g_fprintf (fout, "\n");
423       g_fprintf (fout, "  if (G_CCLOSURE_SWAP_DATA (closure))\n    {\n");
424       g_fprintf (fout, "      data1 = closure->data;\n");
425       g_fprintf (fout, "      data2 = g_value_peek_pointer (param_values + 0);\n");
426       g_fprintf (fout, "    }\n  else\n    {\n");
427       g_fprintf (fout, "      data1 = g_value_peek_pointer (param_values + 0);\n");
428       g_fprintf (fout, "      data2 = closure->data;\n");
429       g_fprintf (fout, "    }\n");
430       g_fprintf (fout, "  callback = (GMarshalFunc_%s) (marshal_data ? marshal_data : cc->callback);\n", signame);
431
432       /* cfile marshal callback action */
433       g_fprintf (fout, "\n");
434       ind = g_fprintf (fout, " %s callback (", sig->rarg->setter ? " v_return =" : "");
435       g_fprintf (fout, "data1,\n");
436       for (a = 1, node = sig->args; node; node = node->next)
437         {
438           InArgument *iarg = node->data;
439
440           if (iarg->getter)
441             g_fprintf (fout, "%s%s (param_values + %d),\n", indent (ind), iarg->getter, a++);
442         }
443       g_fprintf (fout, "%sdata2);\n", indent (ind));
444
445       /* cfile marshal return value storage */
446       if (sig->rarg->setter)
447         {
448           g_fprintf (fout, "\n");
449           g_fprintf (fout, "  %s (return_value, v_return);\n", sig->rarg->setter);
450         }
451
452       /* cfile marshal footer */
453       g_fprintf (fout, "}\n");
454     }
455 }
456
457 static void
458 process_signature (Signature *sig)
459 {
460   gchar *pname, *sname, *tmp;
461   GList *node;
462
463   /* lookup and complete info on arguments */
464   if (!complete_out_arg (sig->rarg))
465     {
466       g_warning ("unknown type: %s", sig->rarg->keyword);
467       return;
468     }
469   for (node = sig->args; node; node = node->next)
470     {
471       InArgument *iarg = node->data;
472
473       if (!complete_in_arg (iarg))
474         {
475           g_warning ("unknown type: %s", iarg->keyword);
476           return;
477         }
478     }
479
480   /* construct requested marshaller name and technical marshaller name */
481   pname = g_strconcat (sig->rarg->keyword, "_", NULL);
482   sname = g_strconcat (sig->rarg->sig_name, "_", NULL);
483   for (node = sig->args; node; node = node->next)
484     {
485       InArgument *iarg = node->data;
486       gchar *tmp;
487
488       tmp = sname;
489       sname = g_strconcat (tmp, "_", iarg->sig_name, NULL);
490       g_free (tmp);
491       tmp = pname;
492       pname = g_strconcat (tmp, "_", iarg->keyword, NULL);
493       g_free (tmp);
494     }
495
496   /* introductionary comment */
497   g_fprintf (fout, "\n/* %s", sig->rarg->keyword);
498   for (node = sig->args; node; node = node->next)
499     {
500       InArgument *iarg = node->data;
501
502       g_fprintf (fout, "%c%s", node->prev ? ',' : ':', iarg->keyword);
503     }
504   if (!skip_ploc)
505     g_fprintf (fout, " (%s)", sig->ploc);
506   g_fprintf (fout, " */\n");
507
508   /* ensure technical marshaller exists (<marshaller_prefix>_<sname>) */
509   generate_marshal (sname, sig);
510
511   /* put out marshaller alias for requested name if required (<marshaller_prefix>_<pname>) */
512   tmp = g_strconcat (marshaller_prefix, "_", pname, NULL);
513   if (gen_cheader && !g_hash_table_lookup (marshallers, tmp))
514     {
515       g_fprintf (fout, "#define %s_%s\t%s_%s\n", marshaller_prefix, pname, marshaller_prefix, sname);
516
517       g_hash_table_insert (marshallers, tmp, tmp);
518     }
519   else
520     g_free (tmp);
521
522   g_free (pname);
523   g_free (sname);
524 }
525
526 static InArgument*
527 new_in_arg (const gchar *pname)
528 {
529   InArgument *iarg = g_new0 (InArgument, 1);
530
531   iarg->keyword = g_strdup (pname);
532
533   return iarg;
534 }
535
536 static OutArgument*
537 new_out_arg (const gchar *pname)
538 {
539   OutArgument *oarg = g_new0 (OutArgument, 1);
540
541   oarg->keyword = g_strdup (pname);
542
543   return oarg;
544 }
545
546 static guint
547 parse_line (GScanner  *scanner,
548             Signature *sig)
549 {
550   /* parse identifier for return value */
551   if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER)
552     return G_TOKEN_IDENTIFIER;
553   sig->rarg = new_out_arg (scanner->value.v_identifier);
554
555   /* keep a note on the location */
556   sig->ploc = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
557
558   /* expect ':' */
559   if (g_scanner_get_next_token (scanner) != ':')
560     return ':';
561
562   /* parse first argument */
563   if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER)
564     return G_TOKEN_IDENTIFIER;
565   sig->args = g_list_append (sig->args, new_in_arg (scanner->value.v_identifier));
566
567   /* parse rest of argument list */
568   while (g_scanner_peek_next_token (scanner) == ',')
569     {
570       /* eat comma */
571       g_scanner_get_next_token (scanner);
572
573       /* parse arg identifier */
574       if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER)
575         return G_TOKEN_IDENTIFIER;
576       sig->args = g_list_append (sig->args, new_in_arg (scanner->value.v_identifier));
577     }
578
579   /* expect end of line, done */
580   if (g_scanner_get_next_token (scanner) != '\n')
581     return '\n';
582
583   /* success */
584   return G_TOKEN_NONE;
585 }
586
587 static gboolean
588 string_key_destroy (gpointer key,
589                     gpointer value,
590                     gpointer user_data)
591 {
592   g_free (key);
593
594   return TRUE;
595 }
596
597 int
598 main (int   argc,
599       char *argv[])
600 {
601   const gchar *gobject_marshallers[] = {
602 #include        "gmarshal.strings"
603   };
604   GScanner *scanner;
605   GSList *slist, *files = NULL;
606   gint i;
607   gint result = 0;
608
609   /* parse args and do fast exits */
610   parse_args (&argc, &argv);
611
612   /* list input files */
613   for (i = 1; i < argc; i++)
614     files = g_slist_prepend (files, argv[i]);
615   if (files)
616     files = g_slist_reverse (files);
617   else
618     files = g_slist_prepend (files, "/dev/stdin");
619
620   /* setup auxillary structs */
621   scanner = g_scanner_new (&scanner_config_template);
622   fout = stdout;
623   marshallers = g_hash_table_new (g_str_hash, g_str_equal);
624
625   /* add standard marshallers of the GObject library */
626   if (std_includes)
627     for (i = 0; i < G_N_ELEMENTS (gobject_marshallers); i++)
628       {
629         gchar *tmp = g_strdup (gobject_marshallers[i]);
630
631         g_hash_table_insert (marshallers, tmp, tmp);
632       }
633
634   /* put out initial heading */
635   g_fprintf (fout, "\n");
636
637   if (gen_cheader && std_includes)
638     {
639       g_fprintf (fout, "#ifndef __%s_MARSHAL_H__\n", marshaller_prefix);
640       g_fprintf (fout, "#define __%s_MARSHAL_H__\n\n", marshaller_prefix);
641     }
642
643   if ((gen_cheader || gen_cbody) && std_includes)
644     g_fprintf (fout, "#include\t<glib-object.h>\n\n");
645
646   if (gen_cheader)
647     g_fprintf (fout, "G_BEGIN_DECLS\n");
648
649   /* generate necessary preprocessor directives */
650   if (gen_cbody)
651     put_marshal_value_getters ();
652
653   /* process input files */
654   for (slist = files; slist; slist = slist->next)
655     {
656       gchar *file = slist->data;
657       gint fd;
658
659       if (strcmp (file, "/dev/stdin") == 0)
660         /* Mostly for Win32. This is equivalent to opening /dev/stdin */
661         fd = dup (0);
662       else
663         fd = open (file, O_RDONLY);
664
665       if (fd < 0)
666         {
667           g_warning ("failed to open \"%s\": %s", file, g_strerror (errno));
668           result = 1;
669           continue;
670         }
671
672       /* set file name for error reports */
673       scanner->input_name = file;
674
675       /* parse & process file */
676       g_scanner_input_file (scanner, fd);
677
678       /* scanning loop, we parse the input untill it's end is reached,
679        * or our sub routine came across invalid syntax
680        */
681       do
682         {
683           guint expected_token = G_TOKEN_NONE;
684
685           switch (g_scanner_peek_next_token (scanner))
686             {
687             case '\n':
688               /* eat newline and restart */
689               g_scanner_get_next_token (scanner);
690               continue;
691             case G_TOKEN_EOF:
692               /* done */
693               break;
694             default:
695               /* parse and process signatures */
696               {
697                 Signature signature = { NULL, NULL, NULL };
698                 GList *node;
699
700                 expected_token = parse_line (scanner, &signature);
701
702                 /* once we got a valid signature, process it */
703                 if (expected_token == G_TOKEN_NONE)
704                   process_signature (&signature);
705
706                 /* clean up signature contents */
707                 g_free (signature.ploc);
708                 if (signature.rarg)
709                   g_free (signature.rarg->keyword);
710                 g_free (signature.rarg);
711                 for (node = signature.args; node; node = node->next)
712                   {
713                     InArgument *iarg = node->data;
714
715                     g_free (iarg->keyword);
716                     g_free (iarg);
717                   }
718                 g_list_free (signature.args);
719               }
720               break;
721             }
722
723           /* bail out on errors */
724           if (expected_token != G_TOKEN_NONE)
725             {
726               g_scanner_unexp_token (scanner, expected_token, "type name", NULL, NULL, NULL, TRUE);
727               result = 1;
728               break;
729             }
730
731           g_scanner_peek_next_token (scanner);
732         }
733       while (scanner->next_token != G_TOKEN_EOF);
734
735       close (fd);
736     }
737
738   /* put out trailer */
739   if (gen_cheader)
740     {
741       g_fprintf (fout, "\nG_END_DECLS\n");
742
743       if (std_includes)
744         g_fprintf (fout, "\n#endif /* __%s_MARSHAL_H__ */\n", marshaller_prefix);
745     }
746   g_fprintf (fout, "\n");
747
748   /* clean up */
749   g_slist_free (files);
750   g_scanner_destroy (scanner);
751   g_hash_table_foreach_remove (marshallers, string_key_destroy, NULL);
752   g_hash_table_destroy (marshallers);
753
754   return result;
755 }
756
757 static void
758 parse_args (gint    *argc_p,
759             gchar ***argv_p)
760 {
761   guint argc = *argc_p;
762   gchar **argv = *argv_p;
763   guint i, e;
764
765   for (i = 1; i < argc; i++)
766     {
767       if (strcmp ("--header", argv[i]) == 0)
768         {
769           gen_cheader = TRUE;
770           argv[i] = NULL;
771         }
772       else if (strcmp ("--body", argv[i]) == 0)
773         {
774           gen_cbody = TRUE;
775           argv[i] = NULL;
776         }
777       else if (strcmp ("--skip-source", argv[i]) == 0)
778         {
779           skip_ploc = TRUE;
780           argv[i] = NULL;
781         }
782       else if (strcmp ("--nostdinc", argv[i]) == 0)
783         {
784           std_includes = FALSE;
785           argv[i] = NULL;
786         }
787       else if (strcmp ("--stdinc", argv[i]) == 0)
788         {
789           std_includes = TRUE;
790           argv[i] = NULL;
791         }
792       else if (strcmp ("--internal", argv[i]) == 0)
793         {
794           gen_internal = TRUE;
795           argv[i] = NULL;
796         }
797       else if ((strcmp ("--prefix", argv[i]) == 0) ||
798                (strncmp ("--prefix=", argv[i], 9) == 0))
799         {
800           gchar *equal = argv[i] + 8;
801
802           if (*equal == '=')
803             marshaller_prefix = g_strdup (equal + 1);
804           else if (i + 1 < argc)
805             {
806               marshaller_prefix = g_strdup (argv[i + 1]);
807               argv[i] = NULL;
808               i += 1;
809             }
810           argv[i] = NULL;
811         }
812       else if (strcmp ("-h", argv[i]) == 0 ||
813           strcmp ("--help", argv[i]) == 0)
814         {
815           print_blurb (stderr, TRUE);
816           argv[i] = NULL;
817           exit (0);
818         }
819       else if (strcmp ("-v", argv[i]) == 0 ||
820                strcmp ("--version", argv[i]) == 0)
821         {
822           print_blurb (stderr, FALSE);
823           argv[i] = NULL;
824           exit (0);
825         }
826       else if (strcmp (argv[i], "--g-fatal-warnings") == 0)
827         {
828           GLogLevelFlags fatal_mask;
829
830           fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
831           fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
832           g_log_set_always_fatal (fatal_mask);
833
834           argv[i] = NULL;
835         }
836     }
837
838   e = 0;
839   for (i = 1; i < argc; i++)
840     {
841       if (e)
842         {
843           if (argv[i])
844             {
845               argv[e++] = argv[i];
846               argv[i] = NULL;
847             }
848         }
849       else if (!argv[i])
850         e = i;
851     }
852   if (e)
853     *argc_p = e;
854 }
855
856 static void
857 print_blurb (FILE    *bout,
858              gboolean print_help)
859 {
860   if (!print_help)
861     {
862       g_fprintf (bout, "%s version ", PRG_NAME);
863       g_fprintf (bout, "%u.%u.%u", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
864       g_fprintf (bout, "\n");
865       g_fprintf (bout, "%s comes with ABSOLUTELY NO WARRANTY.\n", PRG_NAME);
866       g_fprintf (bout, "You may redistribute copies of %s under the terms of\n", PRG_NAME);
867       g_fprintf (bout, "the GNU General Public License which can be found in the\n");
868       g_fprintf (bout, "%s source package. Sources, examples and contact\n", PKG_NAME);
869       g_fprintf (bout, "information are available at %s\n", PKG_HTTP_HOME);
870     }
871   else
872     {
873       g_fprintf (bout, "Usage: %s [options] [files...]\n", PRG_NAME);
874       g_fprintf (bout, "  --header                   generate C headers\n");
875       g_fprintf (bout, "  --body                     generate C code\n");
876       g_fprintf (bout, "  --prefix=string            specify marshaller prefix\n");
877       g_fprintf (bout, "  --skip-source              skip source location comments\n");
878       g_fprintf (bout, "  --stdinc, --nostdinc       include/use standard marshallers\n");
879       g_fprintf (bout, "  --internal                 mark generated functions as internal\n");
880       g_fprintf (bout, "  -h, --help                 show this help message\n");
881       g_fprintf (bout, "  -v, --version              print version informations\n");
882       g_fprintf (bout, "  --g-fatal-warnings         make warnings fatal (abort)\n");
883     }
884 }