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