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