parse: fix segfaulting prototype-mismatch
[platform/upstream/gstreamer.git] / gst / parse / grammar.y
1 %{
2 #include "../gst_private.h"
3
4 #include <glib-object.h>
5 #include <glib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 #include "../gst-i18n-lib.h"
11
12 #include "../gstconfig.h"
13 #include "../gstparse.h"
14 #include "../gstinfo.h"
15 #include "../gsterror.h"
16 #include "../gststructure.h"
17 #include "../gsturi.h"
18 #include "../gstutils.h"
19 #include "../gstvalue.h"
20 #include "../gstchildproxy.h"
21 #include "types.h"
22
23 /* All error messages in this file are user-visible and need to be translated.
24  * Don't start the message with a capital, and don't end them with a period,
25  * as they will be presented inside a sentence/error.
26  */
27
28 #define YYERROR_VERBOSE 1
29
30 #define YYENABLE_NLS 0
31
32 #ifndef YYLTYPE_IS_TRIVIAL
33 #define YYLTYPE_IS_TRIVIAL 0
34 #endif
35
36 typedef void* yyscan_t;
37
38
39 #ifdef __GST_PARSE_TRACE
40 static guint __strings;
41 static guint __links;
42 static guint __chains;
43 gchar *
44 __gst_parse_strdup (gchar *org)
45 {
46   gchar *ret;
47   __strings++;
48   ret = g_strdup (org);
49   /* g_print ("ALLOCATED STR   (%3u): %p %s\n", __strings, ret, ret); */
50   return ret;
51 }
52 void
53 __gst_parse_strfree (gchar *str)
54 {
55   if (str) {
56     /* g_print ("FREEING STR     (%3u): %p %s\n", __strings - 1, str, str); */
57     g_free (str);
58     g_return_if_fail (__strings > 0);
59     __strings--;
60   }
61 }
62 link_t *__gst_parse_link_new ()
63 {
64   link_t *ret;
65   __links++;
66   ret = g_slice_new0 (link_t);
67   /* g_print ("ALLOCATED LINK  (%3u): %p\n", __links, ret); */
68   return ret;
69 }
70 void
71 __gst_parse_link_free (link_t *data)
72 {
73   if (data) {
74     /* g_print ("FREEING LINK    (%3u): %p\n", __links - 1, data); */
75     g_slice_free (link_t, data);
76     g_return_if_fail (__links > 0);
77     __links--;
78   }
79 }
80 chain_t *
81 __gst_parse_chain_new ()
82 {
83   chain_t *ret;
84   __chains++;
85   ret = g_slice_new0 (chain_t);
86   /* g_print ("ALLOCATED CHAIN (%3u): %p\n", __chains, ret); */
87   return ret;
88 }
89 void
90 __gst_parse_chain_free (chain_t *data)
91 {
92   /* g_print ("FREEING CHAIN   (%3u): %p\n", __chains - 1, data); */
93   g_slice_free (chain_t, data);
94   g_return_if_fail (__chains > 0);
95   __chains--;
96 }
97
98 #endif /* __GST_PARSE_TRACE */
99
100 typedef struct {
101   gchar *src_pad;
102   gchar *sink_pad;
103   GstElement *sink;
104   GstCaps *caps;
105   gulong signal_id;
106 } DelayedLink;
107
108 typedef struct {
109   gchar *name;
110   gchar *value_str;
111   gulong signal_id;
112 } DelayedSet;
113
114 /*** define SET_ERROR macro/function */
115
116 #ifdef G_HAVE_ISO_VARARGS
117
118 #  define SET_ERROR(error, type, ...) \
119 G_STMT_START { \
120   GST_CAT_ERROR (GST_CAT_PIPELINE, __VA_ARGS__); \
121   if ((error) && !*(error)) { \
122     g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
123   } \
124 } G_STMT_END
125
126 #elif defined(G_HAVE_GNUC_VARARGS)
127
128 #  define SET_ERROR(error, type, args...) \
129 G_STMT_START { \
130   GST_CAT_ERROR (GST_CAT_PIPELINE, args ); \
131   if ((error) && !*(error)) { \
132     g_set_error ((error), GST_PARSE_ERROR, (type), args ); \
133   } \
134 } G_STMT_END
135
136 #else
137
138 static inline void
139 SET_ERROR (GError **error, gint type, const char *format, ...)
140 {
141   if (error) {
142     if (*error) {
143       g_warning ("error while parsing");
144     } else {
145       va_list varargs;
146       char *string;
147
148       va_start (varargs, format);
149       string = g_strdup_vprintf (format, varargs);
150       va_end (varargs);
151
152       g_set_error (error, GST_PARSE_ERROR, type, string);
153
154       g_free (string);
155     }
156   }
157 }
158
159 #endif /* G_HAVE_ISO_VARARGS */
160
161 /*** define YYPRINTF macro/function if we're debugging */
162
163 /* bison 1.35 calls this macro with side effects, we need to make sure the
164    side effects work - crappy bison */
165
166 #ifndef GST_DISABLE_GST_DEBUG
167 #  define YYDEBUG 1
168
169 #  ifdef G_HAVE_ISO_VARARGS
170
171 /* #  define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__) */
172 #    define YYFPRINTF(a, ...) \
173 G_STMT_START { \
174      GST_CAT_LOG (GST_CAT_PIPELINE, __VA_ARGS__); \
175 } G_STMT_END
176
177 #  elif defined(G_HAVE_GNUC_VARARGS)
178
179 #    define YYFPRINTF(a, args...) \
180 G_STMT_START { \
181      GST_CAT_LOG (GST_CAT_PIPELINE, args); \
182 } G_STMT_END
183
184 #  else
185
186 static inline void
187 YYPRINTF(const char *format, ...)
188 {
189   va_list varargs;
190   gchar *temp;
191
192   va_start (varargs, format);
193   temp = g_strdup_vprintf (format, varargs);
194   GST_CAT_LOG (GST_CAT_PIPELINE, "%s", temp);
195   g_free (temp);
196   va_end (varargs);
197 }
198
199 #  endif /* G_HAVE_ISO_VARARGS */
200
201 #endif /* GST_DISABLE_GST_DEBUG */
202
203
204 /*
205  * include headers generated by bison & flex, after defining (or not defining) YYDEBUG
206  */
207 #include "grammar.tab.h"
208 #include "parse_lex.h"
209
210
211 #define ADD_MISSING_ELEMENT(graph,name) G_STMT_START {                      \
212     if ((graph)->ctx) {                                                     \
213       (graph)->ctx->missing_elements =                                      \
214           g_list_append ((graph)->ctx->missing_elements, g_strdup (name));  \
215     } } G_STMT_END
216
217 static void
218 no_free (gconstpointer foo)
219 {
220   /* do nothing */
221 }
222
223 #define GST_BIN_MAKE(res, type, chainval, assign, type_string_free_func) \
224 G_STMT_START { \
225   chain_t *chain = chainval; \
226   GSList *walk; \
227   GstBin *bin = (GstBin *) gst_element_factory_make (type, NULL); \
228   if (!chain) { \
229     SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY_BIN, \
230         _("specified empty bin \"%s\", not allowed"), type); \
231     g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
232     g_slist_free (assign); \
233     gst_object_unref (bin); \
234     type_string_free_func (type); /* Need to clean up the string */ \
235     YYERROR; \
236   } else if (!bin) { \
237     ADD_MISSING_ELEMENT(graph, type); \
238     SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, \
239         _("no bin \"%s\", skipping"), type); \
240     g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
241     g_slist_free (assign); \
242     res = chain; \
243   } else { \
244     for (walk = chain->elements; walk; walk = walk->next ) \
245       gst_bin_add (bin, GST_ELEMENT (walk->data)); \
246     g_slist_free (chain->elements); \
247     chain->elements = g_slist_prepend (NULL, bin); \
248     res = chain; \
249     /* set the properties now */ \
250     for (walk = assign; walk; walk = walk->next) \
251       gst_parse_element_set ((gchar *) walk->data, GST_ELEMENT (bin), graph); \
252     g_slist_free (assign); \
253   } \
254 } G_STMT_END
255
256 #define MAKE_LINK(link, _src, _src_name, _src_pads, _sink, _sink_name, _sink_pads) \
257 G_STMT_START { \
258   link = gst_parse_link_new (); \
259   link->src = _src; \
260   link->sink = _sink; \
261   link->src_name = _src_name; \
262   link->sink_name = _sink_name; \
263   link->src_pads = _src_pads; \
264   link->sink_pads = _sink_pads; \
265   link->caps = NULL; \
266 } G_STMT_END
267
268 #define MAKE_REF(link, _src, _pads) \
269 G_STMT_START { \
270   gchar *padname = _src; \
271   GSList *pads = _pads; \
272   if (padname) { \
273     while (*padname != '.') padname++; \
274     *padname = '\0'; \
275     padname++; \
276     if (*padname != '\0') \
277       pads = g_slist_prepend (pads, gst_parse_strdup (padname)); \
278   } \
279   MAKE_LINK (link, NULL, _src, pads, NULL, NULL, NULL); \
280 } G_STMT_END
281
282 static void
283 gst_parse_free_delayed_set (DelayedSet *set)
284 {
285   g_free(set->name);
286   g_free(set->value_str);
287   g_slice_free(DelayedSet, set);
288 }
289
290 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
291     const gchar * name, gpointer data);
292
293 static void
294 gst_parse_add_delayed_set (GstElement *element, gchar *name, gchar *value_str)
295 {
296   DelayedSet *data = g_slice_new0 (DelayedSet);
297
298   GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "delaying property set %s to %s",
299     name, value_str);
300
301   data->name = g_strdup(name);
302   data->value_str = g_strdup(value_str);
303   data->signal_id = g_signal_connect_data(element, "child-added",
304       G_CALLBACK (gst_parse_new_child), data, (GClosureNotify)
305       gst_parse_free_delayed_set, (GConnectFlags) 0);
306
307   /* FIXME: we would need to listen on all intermediate bins too */
308   if (GST_IS_BIN (element)) {
309     gchar **names, **current;
310     GstElement *parent, *child;
311
312     current = names = g_strsplit (name, "::", -1);
313     parent = gst_bin_get_by_name (GST_BIN_CAST (element), current[0]);
314     current++;
315     while (parent && current[0]) {
316       child = gst_bin_get_by_name (GST_BIN (parent), current[0]);
317       if (!child && current[1]) {
318         char *sub_name = g_strjoinv ("::", &current[0]);
319
320         gst_parse_add_delayed_set(parent, sub_name, value_str);
321         g_free (sub_name);
322       }
323       parent = child;
324       current++;
325     }
326     g_strfreev (names);
327   }
328 }
329
330 static void
331 gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
332     const gchar * name, gpointer data)
333 {
334   DelayedSet *set = (DelayedSet *) data;
335   GParamSpec *pspec;
336   GValue v = { 0, };
337   GObject *target = NULL;
338   GType value_type;
339
340   GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "new child %s, checking property %s",
341       name, set->name);
342
343   if (gst_child_proxy_lookup (child_proxy, set->name, &target, &pspec)) {
344     gboolean got_value = FALSE;
345
346     value_type = pspec->value_type;
347
348     GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "parsing delayed property %s as a %s from %s",
349       pspec->name, g_type_name (value_type), set->value_str);
350     g_value_init (&v, value_type);
351     if (gst_value_deserialize (&v, set->value_str))
352       got_value = TRUE;
353     else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
354        GstElement *bin;
355
356        bin = gst_parse_bin_from_description_full (set->value_str, TRUE, NULL,
357            GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS, NULL);
358        if (bin) {
359          g_value_set_object (&v, bin);
360          got_value = TRUE;
361        }
362     }
363     g_signal_handler_disconnect (child_proxy, set->signal_id);
364     if (!got_value)
365       goto error;
366     g_object_set_property (target, pspec->name, &v);
367   } else {
368     const gchar *obj_name = GST_OBJECT_NAME(object);
369     gint len = strlen (obj_name);
370
371     /* do a delayed set */
372     if ((strlen (set->name) > (len + 2)) && !strncmp (set->name, obj_name, len) && !strncmp (&set->name[len], "::", 2)) {
373       gst_parse_add_delayed_set (GST_ELEMENT(child_proxy), set->name, set->value_str);
374     }
375   }
376
377 out:
378   if (G_IS_VALUE (&v))
379     g_value_unset (&v);
380   if (target)
381     g_object_unref (target);
382   return;
383
384 error:
385   GST_CAT_ERROR (GST_CAT_PIPELINE, "could not set property \"%s\" in %"
386       GST_PTR_FORMAT, pspec->name, target);
387   goto out;
388 }
389
390 static void
391 gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
392 {
393   GParamSpec *pspec = NULL;
394   gchar *pos = value;
395   GValue v = { 0, };
396   GObject *target = NULL;
397   GType value_type;
398
399   /* do nothing if assignment is for missing element */
400   if (element == NULL)
401     goto out;
402
403   /* parse the string, so the property name is null-terminated and pos points
404      to the beginning of the value */
405   while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++;
406   if (*pos == '=') {
407     *pos = '\0';
408   } else {
409     *pos = '\0';
410     pos++;
411     while (g_ascii_isspace (*pos)) pos++;
412   }
413   pos++;
414   while (g_ascii_isspace (*pos)) pos++;
415   if (*pos == '"') {
416     pos++;
417     pos[strlen (pos) - 1] = '\0';
418   }
419   gst_parse_unescape (pos);
420
421   if (GST_IS_CHILD_PROXY (element)) {
422     if (!gst_child_proxy_lookup (GST_CHILD_PROXY (element), value, &target, &pspec)) {
423       /* do a delayed set */
424       gst_parse_add_delayed_set (element, value, pos);
425     }
426   } else {
427     pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value);
428     if (pspec != NULL) {
429       target = g_object_ref (element);
430       GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, target, "found %s property", value);
431     } else {
432       SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
433           _("no property \"%s\" in element \"%s\""), value, \
434           GST_ELEMENT_NAME (element));
435     }
436   }
437
438   if (pspec != NULL && target != NULL) {
439     gboolean got_value = FALSE;
440
441     value_type = pspec->value_type;
442
443     GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "parsing property %s as a %s",
444         pspec->name, g_type_name (value_type));
445
446     g_value_init (&v, value_type);
447     if (gst_value_deserialize (&v, pos))
448       got_value = TRUE;
449     else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
450        GstElement *bin;
451
452        bin = gst_parse_bin_from_description_full (pos, TRUE, NULL,
453            GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS, NULL);
454        if (bin) {
455          g_value_set_object (&v, bin);
456          got_value = TRUE;
457        }
458     }
459     if (!got_value)
460       goto error;
461     g_object_set_property (target, pspec->name, &v);
462   }
463
464 out:
465   gst_parse_strfree (value);
466   if (G_IS_VALUE (&v))
467     g_value_unset (&v);
468   if (target)
469     g_object_unref (target);
470   return;
471
472 error:
473   SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
474          _("could not set property \"%s\" in element \"%s\" to \"%s\""),
475          value, GST_ELEMENT_NAME (element), pos);
476   goto out;
477 }
478
479 static inline void
480 gst_parse_free_link (link_t *link)
481 {
482   gst_parse_strfree (link->src_name);
483   gst_parse_strfree (link->sink_name);
484   g_slist_foreach (link->src_pads, (GFunc) gst_parse_strfree, NULL);
485   g_slist_foreach (link->sink_pads, (GFunc) gst_parse_strfree, NULL);
486   g_slist_free (link->src_pads);
487   g_slist_free (link->sink_pads);
488   if (link->caps) gst_caps_unref (link->caps);
489   gst_parse_link_free (link);
490 }
491
492 static void
493 gst_parse_free_delayed_link (DelayedLink *link)
494 {
495   g_free (link->src_pad);
496   g_free (link->sink_pad);
497   if (link->caps) gst_caps_unref (link->caps);
498   g_slice_free (DelayedLink, link);
499 }
500
501 static void
502 gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
503 {
504   DelayedLink *link = data;
505
506   GST_CAT_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s",
507                 GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
508                 GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
509
510   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
511       link->sink_pad, link->caps)) {
512     /* do this here, we don't want to get any problems later on when
513      * unlocking states */
514     GST_CAT_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked",
515                    GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
516                    GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
517     g_signal_handler_disconnect (src, link->signal_id);
518   }
519 }
520
521 /* both padnames and the caps may be NULL */
522 static gboolean
523 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
524                                 GstElement *sink, const gchar *sink_pad,
525                                 GstCaps *caps)
526 {
527   GList *templs = gst_element_class_get_pad_template_list (
528       GST_ELEMENT_GET_CLASS (src));
529
530   for (; templs; templs = templs->next) {
531     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
532     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
533         (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
534     {
535       DelayedLink *data = g_slice_new (DelayedLink);
536
537       /* TODO: maybe we should check if src_pad matches this template's names */
538
539       GST_CAT_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s",
540                      GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (src_pad),
541                      GST_STR_NULL (GST_ELEMENT_NAME (sink)), GST_STR_NULL (sink_pad));
542
543       data->src_pad = g_strdup (src_pad);
544       data->sink = sink;
545       data->sink_pad = g_strdup (sink_pad);
546       if (caps) {
547         data->caps = gst_caps_copy (caps);
548       } else {
549         data->caps = NULL;
550       }
551       data->signal_id = g_signal_connect_data (src, "pad-added",
552           G_CALLBACK (gst_parse_found_pad), data,
553           (GClosureNotify) gst_parse_free_delayed_link, (GConnectFlags) 0);
554       return TRUE;
555     }
556   }
557   return FALSE;
558 }
559
560 /*
561  * performs a link and frees the struct. src and sink elements must be given
562  * return values   0 - link performed
563  *                 1 - link delayed
564  *                <0 - error
565  */
566 static gint
567 gst_parse_perform_link (link_t *link, graph_t *graph)
568 {
569   GstElement *src = link->src;
570   GstElement *sink = link->sink;
571   GSList *srcs = link->src_pads;
572   GSList *sinks = link->sink_pads;
573   g_assert (GST_IS_ELEMENT (src));
574   g_assert (GST_IS_ELEMENT (sink));
575
576   GST_CAT_INFO (GST_CAT_PIPELINE,
577       "linking %s:%s to %s:%s (%u/%u) with caps \"%" GST_PTR_FORMAT "\"",
578       GST_ELEMENT_NAME (src), link->src_name ? link->src_name : "(any)",
579       GST_ELEMENT_NAME (sink), link->sink_name ? link->sink_name : "(any)",
580       g_slist_length (srcs), g_slist_length (sinks), link->caps);
581
582   if (!srcs || !sinks) {
583     if (gst_element_link_pads_filtered (src,
584         srcs ? (const gchar *) srcs->data : NULL, sink,
585         sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
586       goto success;
587     } else {
588       if (gst_parse_perform_delayed_link (src,
589           srcs ? (const gchar *) srcs->data : NULL,
590           sink, sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
591         goto success;
592       } else {
593         goto error;
594       }
595     }
596   }
597   if (g_slist_length (link->src_pads) != g_slist_length (link->sink_pads)) {
598     goto error;
599   }
600   while (srcs && sinks) {
601     const gchar *src_pad = (const gchar *) srcs->data;
602     const gchar *sink_pad = (const gchar *) sinks->data;
603     srcs = g_slist_next (srcs);
604     sinks = g_slist_next (sinks);
605     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
606         link->caps)) {
607       continue;
608     } else {
609       if (gst_parse_perform_delayed_link (src, src_pad,
610                                           sink, sink_pad,
611                                           link->caps)) {
612         continue;
613       } else {
614         goto error;
615       }
616     }
617   }
618
619 success:
620   gst_parse_free_link (link);
621   return 0;
622
623 error:
624   SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
625       _("could not link %s to %s"), GST_ELEMENT_NAME (src),
626       GST_ELEMENT_NAME (sink));
627   gst_parse_free_link (link);
628   return -1;
629 }
630
631
632 static int yyerror (void *scanner, graph_t *graph, const char *s);
633 %}
634
635 %union {
636     gchar *s;
637     chain_t *c;
638     link_t *l;
639     GstElement *e;
640     GSList *p;
641     graph_t *g;
642 }
643
644 %token <s> PARSE_URL
645 %token <s> IDENTIFIER
646 %left <s> REF PADREF BINREF
647 %token <s> ASSIGNMENT
648 %token <s> LINK
649
650 %type <g> graph
651 %type <c> chain bin
652 %type <l> reference
653 %type <l> linkpart link
654 %type <p> linklist
655 %type <e> element
656 %type <p> padlist pads assignments
657
658 %left '(' ')'
659 %left ','
660 %right '.'
661 %left '!' '='
662
663 %lex-param { void *scanner }
664 %parse-param { void *scanner }
665 %parse-param { graph_t *graph }
666 %pure-parser
667
668 %start graph
669 %%
670
671 element:        IDENTIFIER                    { $$ = gst_element_factory_make ($1, NULL);
672                                                 if ($$ == NULL) {
673                                                   ADD_MISSING_ELEMENT (graph, $1);
674                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), $1);
675                                                   /* if FATAL_ERRORS flag is set, we don't have to worry about backwards
676                                                    * compatibility and can continue parsing and check for other missing
677                                                    * elements */
678                                                   if ((graph->flags & GST_PARSE_FLAG_FATAL_ERRORS) == 0) {
679                                                     gst_parse_strfree ($1);
680                                                     YYERROR;
681                                                   }
682                                                 }
683                                                 gst_parse_strfree ($1);
684                                               }
685         |       element ASSIGNMENT            { gst_parse_element_set ($2, $1, graph);
686                                                 $$ = $1;
687                                               }
688         ;
689 assignments:    /* NOP */                     { $$ = NULL; }
690         |       assignments ASSIGNMENT        { $$ = g_slist_prepend ($1, $2); }
691         ;
692 bin:            '(' assignments chain ')' { GST_BIN_MAKE ($$, "bin", $3, $2, no_free); }
693         |       BINREF assignments chain ')'  { GST_BIN_MAKE ($$, $1, $3, $2, gst_parse_strfree);
694                                                 gst_parse_strfree ($1);
695                                               }
696         |       BINREF assignments ')'        { GST_BIN_MAKE ($$, $1, NULL, $2, gst_parse_strfree);
697                                                 gst_parse_strfree ($1);
698                                               }
699         |       BINREF assignments error ')'  { GST_BIN_MAKE ($$, $1, NULL, $2, gst_parse_strfree);
700                                                 gst_parse_strfree ($1);
701                                               }
702         ;
703
704 pads:           PADREF                        { $$ = g_slist_prepend (NULL, $1); }
705         |       PADREF padlist                { $$ = $2;
706                                                 $$ = g_slist_prepend ($$, $1);
707                                               }
708         ;
709 padlist:        ',' IDENTIFIER                { $$ = g_slist_prepend (NULL, $2); }
710         |       ',' IDENTIFIER padlist        { $$ = g_slist_prepend ($3, $2); }
711         ;
712
713 reference:      REF                           { MAKE_REF ($$, $1, NULL); }
714         |       REF padlist                   { MAKE_REF ($$, $1, $2); }
715         ;
716
717 linkpart:       reference                     { $$ = $1; }
718         |       pads                          { MAKE_REF ($$, NULL, $1); }
719         |       /* NOP */                     { MAKE_REF ($$, NULL, NULL); }
720         ;
721
722 link:           linkpart LINK linkpart        { $$ = $1;
723                                                 if ($2) {
724                                                   $$->caps = gst_caps_from_string ($2);
725                                                   if ($$->caps == NULL)
726                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $2);
727                                                   gst_parse_strfree ($2);
728                                                 }
729                                                 $$->sink_name = $3->src_name;
730                                                 $$->sink_pads = $3->src_pads;
731                                                 gst_parse_link_free ($3);
732                                               }
733         ;
734
735 linklist:       link                          { $$ = g_slist_prepend (NULL, $1); }
736         |       link linklist                 { $$ = g_slist_prepend ($2, $1); }
737         |       linklist error                { $$ = $1; }
738         ;
739
740 chain:          element                       { $$ = gst_parse_chain_new ();
741                                                 $$->first = $$->last = $1;
742                                                 $$->front = $$->back = NULL;
743                                                 $$->elements = g_slist_prepend (NULL, $1);
744                                               }
745         |       bin                           { $$ = $1; }
746         |       chain chain                   { if ($1->back && $2->front) {
747                                                   if (!$1->back->sink_name) {
748                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
749                                                     gst_parse_free_link ($1->back);
750                                                   } else {
751                                                     graph->links = g_slist_prepend (graph->links, $1->back);
752                                                   }
753                                                   if (!$2->front->src_name) {
754                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
755                                                     gst_parse_free_link ($2->front);
756                                                   } else {
757                                                     graph->links = g_slist_prepend (graph->links, $2->front);
758                                                   }
759                                                   $1->back = NULL;
760                                                 } else if ($1->back) {
761                                                   if (!$1->back->sink_name) {
762                                                     $1->back->sink = $2->first;
763                                                   }
764                                                 } else if ($2->front) {
765                                                   if (!$2->front->src_name) {
766                                                     $2->front->src = $1->last;
767                                                   }
768                                                   $1->back = $2->front;
769                                                 }
770
771                                                 if ($1->back) {
772                                                   graph->links = g_slist_prepend (graph->links, $1->back);
773                                                 }
774                                                 $1->last = $2->last;
775                                                 $1->back = $2->back;
776                                                 $1->elements = g_slist_concat ($1->elements, $2->elements);
777                                                 if ($2)
778                                                   gst_parse_chain_free ($2);
779                                                 $$ = $1;
780                                               }
781         |       chain linklist                { GSList *walk;
782                                                 if ($1->back) {
783                                                   $2 = g_slist_prepend ($2, $1->back);
784                                                   $1->back = NULL;
785                                                 } else {
786                                                   if (!((link_t *) $2->data)->src_name) {
787                                                     ((link_t *) $2->data)->src = $1->last;
788                                                   }
789                                                 }
790                                                 for (walk = $2; walk; walk = walk->next) {
791                                                   link_t *link = (link_t *) walk->data;
792                                                   if (!link->sink_name && walk->next) {
793                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
794                                                     gst_parse_free_link (link);
795                                                   } else if (!link->src_name && !link->src) {
796                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
797                                                     gst_parse_free_link (link);
798                                                   } else {
799                                                     if (walk->next) {
800                                                       graph->links = g_slist_prepend (graph->links, link);
801                                                     } else {
802                                                       $1->back = link;
803                                                     }
804                                                   }
805                                                 }
806                                                 g_slist_free ($2);
807                                                 $$ = $1;
808                                               }
809         |       chain error                   { $$ = $1; }
810         |       link chain                    { if ($2->front) {
811                                                   if (!$2->front->src_name) {
812                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
813                                                     gst_parse_free_link ($2->front);
814                                                   } else {
815                                                     graph->links = g_slist_prepend (graph->links, $2->front);
816                                                   }
817                                                 }
818                                                 if (!$1->sink_name) {
819                                                   $1->sink = $2->first;
820                                                 }
821                                                 $2->front = $1;
822                                                 $$ = $2;
823                                               }
824         |       PARSE_URL chain               { $$ = $2;
825                                                 if ($$->front) {
826                                                   GstElement *element =
827                                                           gst_element_make_from_uri (GST_URI_SRC, $1, NULL, NULL);
828                                                   /* FIXME: get and parse error properly */
829                                                   if (!element) {
830                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
831                                                             _("no source element for URI \"%s\""), $1);
832                                                   } else {
833                                                     $$->front->src = element;
834                                                     graph->links = g_slist_prepend (
835                                                             graph->links, $$->front);
836                                                     $$->front = NULL;
837                                                     $$->elements = g_slist_prepend ($$->elements, element);
838                                                   }
839                                                 } else {
840                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
841                                                           _("no element to link URI \"%s\" to"), $1);
842                                                 }
843                                                 g_free ($1);
844                                               }
845         |       link PARSE_URL                { GstElement *element =
846                                                           gst_element_make_from_uri (GST_URI_SINK, $2, NULL, NULL);
847                                                 /* FIXME: get and parse error properly */
848                                                 if (!element) {
849                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
850                                                           _("no sink element for URI \"%s\""), $2);
851                                                   gst_parse_link_free ($1);
852                                                   g_free ($2);
853                                                   YYERROR;
854                                                 } else if ($1->sink_name || $1->sink_pads) {
855                                                   gst_object_unref (element);
856                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
857                                                           _("could not link sink element for URI \"%s\""), $2);
858                                                   gst_parse_link_free ($1);
859                                                   g_free ($2);
860                                                   YYERROR;
861                                                 } else {
862                                                   $$ = gst_parse_chain_new ();
863                                                   $$->first = $$->last = element;
864                                                   $$->front = $1;
865                                                   $$->front->sink = element;
866                                                   $$->elements = g_slist_prepend (NULL, element);
867                                                 }
868                                                 g_free ($2);
869                                               }
870         ;
871 graph:          /* NOP */                     { SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
872                                                 $$ = graph;
873                                               }
874         |       chain                         { $$ = graph;
875                                                 if ($1->front) {
876                                                   if (!$1->front->src_name) {
877                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
878                                                     gst_parse_free_link ($1->front);
879                                                   } else {
880                                                     $$->links = g_slist_prepend ($$->links, $1->front);
881                                                   }
882                                                   $1->front = NULL;
883                                                 }
884                                                 if ($1->back) {
885                                                   if (!$1->back->sink_name) {
886                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
887                                                     gst_parse_free_link ($1->back);
888                                                   } else {
889                                                     $$->links = g_slist_prepend ($$->links, $1->back);
890                                                   }
891                                                   $1->back = NULL;
892                                                 }
893                                                 $$->chain = $1;
894                                               }
895         ;
896
897 %%
898
899
900 static int
901 yyerror (void *scanner, graph_t *graph, const char *s)
902 {
903   /* FIXME: This should go into the GError somehow, but how? */
904   GST_WARNING ("Error during parsing: %s", s);
905   return -1;
906 }
907
908
909 GstElement *
910 priv_gst_parse_launch (const gchar *str, GError **error, GstParseContext *ctx,
911     GstParseFlags flags)
912 {
913   graph_t g;
914   gchar *dstr;
915   GSList *walk;
916   GstBin *bin = NULL;
917   GstElement *ret;
918   yyscan_t scanner;
919
920   g_return_val_if_fail (str != NULL, NULL);
921   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
922
923   g.chain = NULL;
924   g.links = NULL;
925   g.error = error;
926   g.ctx = ctx;
927   g.flags = flags;
928
929 #ifdef __GST_PARSE_TRACE
930   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
931   __strings = __chains = __links = 0;
932 #endif /* __GST_PARSE_TRACE */
933
934   dstr = g_strdup (str);
935   priv_gst_parse_yylex_init (&scanner);
936   priv_gst_parse_yy_scan_string (dstr, scanner);
937
938 #if YYDEBUG
939   yydebug = 1;
940 #endif
941
942   if (yyparse (scanner, &g) != 0) {
943     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
944         "Unrecoverable syntax error while parsing pipeline %s", str);
945
946     priv_gst_parse_yylex_destroy (scanner);
947     g_free (dstr);
948
949     goto error1;
950   }
951   priv_gst_parse_yylex_destroy (scanner);
952   g_free (dstr);
953
954   GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
955       g.chain ? g_slist_length (g.chain->elements) : 0,
956       g_slist_length (g.links));
957
958   if (!g.chain) {
959     ret = NULL;
960   } else if (!g.chain->elements->next) {
961     /* only one toplevel element */
962     ret = (GstElement *) g.chain->elements->data;
963     g_slist_free (g.chain->elements);
964     if (GST_IS_BIN (ret))
965       bin = GST_BIN (ret);
966     gst_parse_chain_free (g.chain);
967   } else {
968     /* put all elements in our bin */
969     bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
970     g_assert (bin);
971
972     for (walk = g.chain->elements; walk; walk = walk->next) {
973       if (walk->data != NULL)
974         gst_bin_add (bin, GST_ELEMENT (walk->data));
975     }
976
977     g_slist_free (g.chain->elements);
978     ret = GST_ELEMENT (bin);
979     gst_parse_chain_free (g.chain);
980   }
981
982   /* remove links */
983   for (walk = g.links; walk; walk = walk->next) {
984     link_t *l = (link_t *) walk->data;
985     if (!l->src) {
986       if (l->src_name) {
987         if (bin) {
988           l->src = gst_bin_get_by_name_recurse_up (bin, l->src_name);
989           if (l->src)
990             gst_object_unref (l->src);
991         } else {
992           l->src = strcmp (GST_ELEMENT_NAME (ret), l->src_name) == 0 ? ret : NULL;
993         }
994       }
995       if (!l->src) {
996         if (l->src_name) {
997           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
998               "No element named \"%s\" - omitting link", l->src_name);
999         } else {
1000           /* probably a missing element which we've handled already */
1001         }
1002         gst_parse_free_link (l);
1003         continue;
1004       }
1005     }
1006     if (!l->sink) {
1007       if (l->sink_name) {
1008         if (bin) {
1009           l->sink = gst_bin_get_by_name_recurse_up (bin, l->sink_name);
1010           if (l->sink)
1011             gst_object_unref (l->sink);
1012         } else {
1013           l->sink = strcmp (GST_ELEMENT_NAME (ret), l->sink_name) == 0 ? ret : NULL;
1014         }
1015       }
1016       if (!l->sink) {
1017         if (l->sink_name) {
1018           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1019               "No element named \"%s\" - omitting link", l->sink_name);
1020         } else {
1021           /* probably a missing element which we've handled already */
1022         }
1023         gst_parse_free_link (l);
1024         continue;
1025       }
1026     }
1027     gst_parse_perform_link (l, &g);
1028   }
1029   g_slist_free (g.links);
1030
1031 out:
1032 #ifdef __GST_PARSE_TRACE
1033   GST_CAT_DEBUG (GST_CAT_PIPELINE,
1034       "TRACE: %u strings, %u chains and %u links left", __strings, __chains,
1035       __links);
1036   if (__strings || __chains || __links) {
1037     g_warning ("TRACE: %u strings, %u chains and %u links left", __strings,
1038         __chains, __links);
1039   }
1040 #endif /* __GST_PARSE_TRACE */
1041
1042   return ret;
1043
1044 error1:
1045   if (g.chain) {
1046     g_slist_foreach (g.chain->elements, (GFunc)gst_object_unref, NULL);
1047     g_slist_free (g.chain->elements);
1048     gst_parse_chain_free (g.chain);
1049   }
1050
1051   g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
1052   g_slist_free (g.links);
1053
1054   if (error)
1055     g_assert (*error);
1056   ret = NULL;
1057
1058   goto out;
1059 }