gst/: Fix 2 caps leaks.
[platform/upstream/gstreamer.git] / gst / parse / grammar.y
1 %{
2 #include <glib-object.h>
3 #include <glib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 #include "../gst_private.h"
9 #include "../gst-i18n-lib.h"
10
11 #include "../gstconfig.h"
12 #include "../gstparse.h"
13 #include "../gstinfo.h"
14 #include "../gsterror.h"
15 #include "../gsturi.h"
16 #include "../gstutils.h"
17 #include "../gstvalue.h"
18 #include "../gstchildproxy.h"
19 #include "types.h"
20
21 /* All error messages in this file are user-visible and need to be translated.
22  * Don't start the message with a capital, and don't end them with a period,
23  * as they will be presented inside a sentence/error.
24  */
25   
26 #define YYERROR_VERBOSE 1
27 #define YYLEX_PARAM scanner
28
29 typedef void* yyscan_t;
30
31 int _gst_parse_yylex (void * yylval_param , yyscan_t yyscanner);
32 int _gst_parse_yylex_init (yyscan_t scanner);
33 int _gst_parse_yylex_destroy (yyscan_t scanner);
34 struct yy_buffer_state * _gst_parse_yy_scan_string (char* , yyscan_t);
35 void _gst_parse_yypush_buffer_state (void * new_buffer ,yyscan_t yyscanner );
36 void _gst_parse_yypop_buffer_state (yyscan_t yyscanner );
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_new0 (link_t, 1);
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_free (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_new0 (chain_t, 1);
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_free (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   /* FIXME: need to connect to "disposed" signal to clean up,
107    * but there is no such signal */
108 } DelayedLink;
109
110 typedef struct {
111   GstElement *parent;
112   gchar *name;
113   gchar *value_str;
114   gulong signal_id;
115 } DelayedSet;
116
117 /*** define SET_ERROR and ERROR macros/functions */
118
119 #ifdef G_HAVE_ISO_VARARGS
120
121 #  define SET_ERROR(error, type, ...) \
122 G_STMT_START { \
123   GST_CAT_ERROR (GST_CAT_PIPELINE, __VA_ARGS__); \
124   if ((error) && !*(error)) { \
125     g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
126   } \
127 } G_STMT_END
128
129 #  define ERROR(type, ...) \
130   SET_ERROR (((graph_t *) graph)->error, (type), __VA_ARGS__ )
131
132 #elif defined(G_HAVE_GNUC_VARARGS)
133
134 #  define SET_ERROR(error, type, args...) \
135 G_STMT_START { \
136   GST_CAT_ERROR (GST_CAT_PIPELINE, args ); \
137   if ((error) && !*(error)) { \
138     g_set_error ((error), GST_PARSE_ERROR, (type), args ); \
139   } \
140 } G_STMT_END
141
142 #  define ERROR(type, args...) \
143   SET_ERROR (((graph_t *) graph)->error,(type) , args )
144
145 #else
146
147 static inline void
148 SET_ERROR (GError **error, gint type, const char *format, ...)
149 {
150   if (error) {
151     if (*error) {
152       g_warning ("error while parsing");
153     } else {
154       va_list varargs;
155       char *string;
156
157       va_start (varargs, format);
158       string = g_strdup_vprintf (format, varargs);
159       va_end (varargs);
160       
161       g_set_error (error, GST_PARSE_ERROR, type, string);
162
163       g_free (string);
164     }
165   }
166 }
167
168 #endif /* G_HAVE_ISO_VARARGS */
169
170 /*** define YYPRINTF macro/function if we're debugging */
171
172 /* bison 1.35 calls this macro with side effects, we need to make sure the
173    side effects work - crappy bison */
174
175 #ifndef GST_DISABLE_GST_DEBUG
176 #  define YYDEBUG 1
177
178 #  ifdef G_HAVE_ISO_VARARGS
179
180 /* #  define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__) */
181 #    define YYFPRINTF(a, ...) \
182 G_STMT_START { \
183      gchar *temp = g_strdup_printf (__VA_ARGS__); \
184      GST_CAT_LOG (GST_CAT_PIPELINE, temp); \
185      g_free (temp); \
186 } G_STMT_END
187
188 #  elif defined(G_HAVE_GNUC_VARARGS)
189
190 #    define YYFPRINTF(a, args...) \
191 G_STMT_START { \
192      gchar *temp = g_strdup_printf ( args ); \
193      GST_CAT_LOG (GST_CAT_PIPELINE, temp); \
194      g_free (temp); \
195 } G_STMT_END
196
197 #  else
198
199 static inline void
200 YYPRINTF(const char *format, ...)
201 {
202   va_list varargs;
203   gchar *temp;
204   
205   va_start (varargs, format);
206   temp = g_strdup_vprintf (format, varargs);
207   GST_CAT_LOG (GST_CAT_PIPELINE, "%s", temp);
208   g_free (temp);
209   va_end (varargs);
210 }
211
212 #  endif /* G_HAVE_ISO_VARARGS */
213
214 #endif /* GST_DISABLE_GST_DEBUG */
215
216 #define GST_BIN_MAKE(res, type, chainval, assign, free_string) \
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_t *) 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     if (free_string) \
228       gst_parse_strfree (type); /* Need to clean up the string */ \
229     YYERROR; \
230   } else if (!bin) { \
231     SET_ERROR (((graph_t *) 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 gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
276                                 gpointer data)
277 {
278   DelayedSet *set = (DelayedSet *) data;
279   GParamSpec *pspec;
280   GValue v = { 0, }; 
281   GstObject *target = NULL;
282   GType value_type;
283
284   if (gst_child_proxy_lookup (GST_OBJECT (set->parent), set->name, &target, &pspec)) { 
285     value_type = G_PARAM_SPEC_VALUE_TYPE (pspec);
286
287     GST_CAT_LOG (GST_CAT_PIPELINE, "parsing delayed property %s as a %s from %s", pspec->name,
288       g_type_name (value_type), set->value_str);
289     g_value_init (&v, value_type);
290     if (gst_value_deserialize (&v, set->value_str)) {
291       g_object_set_property (G_OBJECT (target), pspec->name, &v);
292     }
293     g_signal_handler_disconnect (child_proxy, set->signal_id);
294     g_free(set->name);
295     g_free(set->value_str);
296     g_free(set);
297   }
298
299   if (G_IS_VALUE (&v))
300     g_value_unset (&v);
301   if (target)
302     gst_object_unref (target);
303   return;
304 }
305
306
307 static void
308 gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
309 {
310   GParamSpec *pspec;
311   gchar *pos = value;
312   GValue v = { 0, }; 
313   GstObject *target = NULL;
314   GType value_type;
315
316   /* parse the string, so the property name is null-terminated an pos points
317      to the beginning of the value */
318   while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++; 
319   if (*pos == '=') { 
320     *pos = '\0'; 
321   } else { 
322     *pos = '\0'; 
323     pos++;
324     while (g_ascii_isspace (*pos)) pos++; 
325   } 
326   pos++; 
327   while (g_ascii_isspace (*pos)) pos++; 
328   if (*pos == '"') {
329     pos++;
330     pos[strlen (pos) - 1] = '\0';
331   }
332   gst_parse_unescape (pos);
333
334   if (gst_child_proxy_lookup (GST_OBJECT (element), value, &target, &pspec)) { 
335     value_type = G_PARAM_SPEC_VALUE_TYPE (pspec); 
336     GST_CAT_LOG (GST_CAT_PIPELINE, "parsing property %s as a %s", pspec->name,
337       g_type_name (value_type));
338     g_value_init (&v, value_type);
339     if (!gst_value_deserialize (&v, pos))
340       goto error;
341     g_object_set_property (G_OBJECT (target), pspec->name, &v);
342     g_value_unset (&v);
343   } else { 
344     /* do a delayed set */
345     if (GST_IS_CHILD_PROXY (element)) {
346       DelayedSet *data = g_new (DelayedSet, 1);
347       
348       data->parent = element;
349       data->name = g_strdup(value);
350       data->value_str = g_strdup(pos);
351       data->signal_id = g_signal_connect(GST_OBJECT (element),"child-added", G_CALLBACK (gst_parse_new_child), data);
352     }
353     else {
354       SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
355           _("no property \"%s\" in element \"%s\""), value, \
356           GST_ELEMENT_NAME (element));
357     }
358   }
359
360 out:
361   gst_parse_strfree (value);
362   if (G_IS_VALUE (&v))
363     g_value_unset (&v);
364   if (target)
365     gst_object_unref (target);
366   return;
367   
368 error:
369   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
370          _("could not set property \"%s\" in element \"%s\" to \"%s\""), 
371          value, GST_ELEMENT_NAME (element), pos); 
372   goto out;
373 }
374 static inline void
375 gst_parse_free_link (link_t *link)
376 {
377   gst_parse_strfree (link->src_name);
378   gst_parse_strfree (link->sink_name);
379   g_slist_foreach (link->src_pads, (GFunc) gst_parse_strfree, NULL);
380   g_slist_foreach (link->sink_pads, (GFunc) gst_parse_strfree, NULL);
381   g_slist_free (link->src_pads);
382   g_slist_free (link->sink_pads);
383   if (link->caps) gst_caps_unref (link->caps);
384   gst_parse_link_free (link);  
385 }
386
387 static void
388 gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
389 {
390   DelayedLink *link = (DelayedLink *) data;
391   
392   GST_CAT_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s", 
393                 GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
394                 GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
395
396   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
397       link->sink_pad, link->caps)) {
398     /* do this here, we don't want to get any problems later on when
399      * unlocking states */
400     GST_CAT_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked", 
401                    GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
402                    GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
403     g_signal_handler_disconnect (src, link->signal_id);
404     g_free (link->src_pad);
405     g_free (link->sink_pad);
406     if (link->caps) gst_caps_unref (link->caps);
407     g_free (link);
408   }
409 }
410 /* both padnames and the caps may be NULL */
411 static gboolean
412 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad, 
413                                 GstElement *sink, const gchar *sink_pad,
414                                 GstCaps *caps)
415 {
416   GList *templs = gst_element_class_get_pad_template_list (
417       GST_ELEMENT_GET_CLASS (src));
418          
419   for (; templs; templs = templs->next) {
420     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
421     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
422         (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
423     {
424       DelayedLink *data = g_new (DelayedLink, 1); 
425       
426       /* TODO: maybe we should check if src_pad matches this template's names */
427
428       GST_CAT_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s", 
429                      GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (src_pad),
430                      GST_STR_NULL (GST_ELEMENT_NAME (sink)), GST_STR_NULL (sink_pad));
431
432       data->src_pad = g_strdup (src_pad);
433       data->sink = sink;
434       data->sink_pad = g_strdup (sink_pad);
435       if (caps) {
436         data->caps = gst_caps_copy (caps);
437       } else {
438         data->caps = NULL;
439       }
440       data->signal_id = g_signal_connect (G_OBJECT (src), "pad-added", 
441           G_CALLBACK (gst_parse_found_pad), data);
442       return TRUE;
443     }
444   }
445   return FALSE;
446 }
447 /*
448  * performs a link and frees the struct. src and sink elements must be given
449  * return values   0 - link performed
450  *                 1 - link delayed
451  *                <0 - error
452  */
453 static gint
454 gst_parse_perform_link (link_t *link, graph_t *graph)
455 {
456   GstElement *src = link->src;
457   GstElement *sink = link->sink;
458   GSList *srcs = link->src_pads;
459   GSList *sinks = link->sink_pads;
460   g_assert (GST_IS_ELEMENT (src));
461   g_assert (GST_IS_ELEMENT (sink));
462   
463   GST_CAT_INFO (GST_CAT_PIPELINE,
464       "linking %s:%s to %s:%s (%u/%u) with caps \"%" GST_PTR_FORMAT "\"", 
465       GST_ELEMENT_NAME (src), link->src_name ? link->src_name : "(any)",
466       GST_ELEMENT_NAME (sink), link->sink_name ? link->sink_name : "(any)",
467       g_slist_length (srcs), g_slist_length (sinks), link->caps);
468
469   if (!srcs || !sinks) {
470     if (gst_element_link_pads_filtered (src,
471         srcs ? (const gchar *) srcs->data : NULL, sink,
472         sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
473       goto success;
474     } else {
475       if (gst_parse_perform_delayed_link (src,
476           srcs ? (const gchar *) srcs->data : NULL,
477           sink, sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
478         goto success;
479       } else {
480         goto error;
481       }
482     }
483   }
484   if (g_slist_length (link->src_pads) != g_slist_length (link->src_pads)) {
485     goto error;
486   }
487   while (srcs && sinks) {
488     const gchar *src_pad = (const gchar *) srcs->data;
489     const gchar *sink_pad = (const gchar *) sinks->data;
490     srcs = g_slist_next (srcs);
491     sinks = g_slist_next (sinks);
492     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
493         link->caps)) {
494       continue;
495     } else {
496       if (gst_parse_perform_delayed_link (src, src_pad,
497                                           sink, sink_pad,
498                                           link->caps)) {
499         continue;
500       } else {
501         goto error;
502       }
503     }
504   }
505   
506 success:
507   gst_parse_free_link (link);
508   return 0;
509   
510 error:
511   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK,
512       _("could not link %s to %s"), GST_ELEMENT_NAME (src),
513       GST_ELEMENT_NAME (sink));
514   gst_parse_free_link (link);
515   return -1;
516 }
517
518
519 static int yyerror (void *scanner, graph_t *graph, const char *s);
520 %}
521
522 %union {
523     gchar *s;
524     chain_t *c;
525     link_t *l;
526     GstElement *e;
527     GSList *p;
528     graph_t *g;
529 }
530
531 %token <s> PARSE_URL
532 %token <s> IDENTIFIER
533 %left <s> REF PADREF BINREF
534 %token <s> ASSIGNMENT
535 %token <s> LINK
536
537 %type <g> graph
538 %type <c> chain bin
539 %type <l> reference
540 %type <l> linkpart link
541 %type <p> linklist
542 %type <e> element 
543 %type <p> padlist pads assignments
544
545 %left '(' ')'
546 %left ','
547 %right '.'
548 %left '!' '='
549
550 %parse-param { void *scanner }
551 %parse-param { graph_t *graph }
552 %pure-parser
553
554 %start graph
555 %%
556
557 element:        IDENTIFIER                    { $$ = gst_element_factory_make ($1, NULL); 
558                                                 if ($$ == NULL) {
559                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), $1);
560                                                   gst_parse_strfree ($1);
561                                                   YYERROR;
562                                                 }
563                                                 gst_parse_strfree ($1);
564                                               }
565         |       element ASSIGNMENT            { gst_parse_element_set ($2, $1, graph);
566                                                 $$ = $1;
567                                               }
568         ;
569 assignments:    /* NOP */                     { $$ = NULL; }
570         |       assignments ASSIGNMENT        { $$ = g_slist_prepend ($1, $2); }
571         ;               
572 bin:            '(' assignments chain ')' { GST_BIN_MAKE ($$, "bin", $3, $2, FALSE); }
573         |       BINREF assignments chain ')'  { GST_BIN_MAKE ($$, $1, $3, $2, TRUE); 
574                                                 gst_parse_strfree ($1);
575                                               }
576         |       BINREF assignments ')'        { GST_BIN_MAKE ($$, $1, NULL, $2, TRUE); 
577                                                 gst_parse_strfree ($1);
578                                               }
579         |       BINREF assignments error ')'  { GST_BIN_MAKE ($$, $1, NULL, $2, TRUE); 
580                                                 gst_parse_strfree ($1);
581                                               }
582         ;
583         
584 pads:           PADREF                        { $$ = g_slist_prepend (NULL, $1); }
585         |       PADREF padlist                { $$ = $2;
586                                                 $$ = g_slist_prepend ($$, $1);
587                                               }                              
588         ;
589 padlist:        ',' IDENTIFIER                { $$ = g_slist_prepend (NULL, $2); }
590         |       ',' IDENTIFIER padlist        { $$ = g_slist_prepend ($3, $2); }
591         ;
592         
593 reference:      REF                           { MAKE_REF ($$, $1, NULL); }
594         |       REF padlist                   { MAKE_REF ($$, $1, $2); }
595         ;
596
597 linkpart:       reference                     { $$ = $1; }
598         |       pads                          { MAKE_REF ($$, NULL, $1); }
599         |       /* NOP */                     { MAKE_REF ($$, NULL, NULL); }
600         ;
601         
602 link:           linkpart LINK linkpart        { $$ = $1;
603                                                 if ($2) {
604                                                   $$->caps = gst_caps_from_string ($2);
605                                                   if ($$->caps == NULL)
606                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $2);
607                                                   gst_parse_strfree ($2);
608                                                 }
609                                                 $$->sink_name = $3->src_name;
610                                                 $$->sink_pads = $3->src_pads;
611                                                 gst_parse_link_free ($3);
612                                               }
613         ;
614         
615 linklist:       link                          { $$ = g_slist_prepend (NULL, $1); }
616         |       link linklist                 { $$ = g_slist_prepend ($2, $1); }
617         |       linklist error                { $$ = $1; }
618         ;       
619
620 chain:          element                       { $$ = gst_parse_chain_new ();
621                                                 $$->first = $$->last = $1;
622                                                 $$->front = $$->back = NULL;
623                                                 $$->elements = g_slist_prepend (NULL, $1);
624                                               }
625         |       bin                           { $$ = $1; }
626         |       chain chain                   { if ($1->back && $2->front) {
627                                                   if (!$1->back->sink_name) {
628                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
629                                                     gst_parse_free_link ($1->back);
630                                                   } else {
631                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
632                                                   }
633                                                   if (!$2->front->src_name) {
634                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
635                                                     gst_parse_free_link ($2->front);
636                                                   } else {
637                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
638                                                   }
639                                                   $1->back = NULL;
640                                                 } else if ($1->back) {
641                                                   if (!$1->back->sink_name) {
642                                                     $1->back->sink = $2->first;
643                                                   }
644                                                 } else if ($2->front) {
645                                                   if (!$2->front->src_name) {
646                                                     $2->front->src = $1->last;
647                                                   }
648                                                   $1->back = $2->front;
649                                                 }
650                                                 
651                                                 if ($1->back) {
652                                                   ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
653                                                 }
654                                                 $1->last = $2->last;
655                                                 $1->back = $2->back;
656                                                 $1->elements = g_slist_concat ($1->elements, $2->elements);
657                                                 if ($2)
658                                                   gst_parse_chain_free ($2);
659                                                 $$ = $1;
660                                               }
661         |       chain linklist                { GSList *walk;
662                                                 if ($1->back) {
663                                                   $2 = g_slist_prepend ($2, $1->back);
664                                                   $1->back = NULL;
665                                                 } else {
666                                                   if (!((link_t *) $2->data)->src_name) {
667                                                     ((link_t *) $2->data)->src = $1->last;
668                                                   }                                               
669                                                 }
670                                                 for (walk = $2; walk; walk = walk->next) {
671                                                   link_t *link = (link_t *) walk->data;
672                                                   if (!link->sink_name && walk->next) {
673                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
674                                                     gst_parse_free_link (link);
675                                                   } else if (!link->src_name && !link->src) {
676                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
677                                                     gst_parse_free_link (link);
678                                                   } else {
679                                                     if (walk->next) {
680                                                       ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, link);
681                                                     } else {
682                                                       $1->back = link;
683                                                     }
684                                                   }
685                                                 }
686                                                 g_slist_free ($2);
687                                                 $$ = $1;
688                                               }
689         |       chain error                   { $$ = $1; }
690         |       link chain                    { if ($2->front) {
691                                                   if (!$2->front->src_name) {
692                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
693                                                     gst_parse_free_link ($2->front);
694                                                   } else {
695                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
696                                                   }
697                                                 }
698                                                 if (!$1->sink_name) {
699                                                   $1->sink = $2->first;
700                                                 }
701                                                 $2->front = $1;
702                                                 $$ = $2;
703                                               }
704         |       PARSE_URL chain               { $$ = $2;
705                                                 if ($$->front) {
706                                                   GstElement *element = 
707                                                           gst_element_make_from_uri (GST_URI_SRC, $1, NULL);
708                                                   if (!element) {
709                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, 
710                                                             _("no source element for URI \"%s\""), $1);
711                                                   } else {
712                                                     $$->front->src = element;
713                                                     ((graph_t *) graph)->links = g_slist_prepend (
714                                                             ((graph_t *) graph)->links, $$->front);
715                                                     $$->front = NULL;
716                                                     $$->elements = g_slist_prepend ($$->elements, element);
717                                                   }
718                                                 } else {
719                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, 
720                                                           _("no element to link URI \"%s\" to"), $1);
721                                                 }
722                                                 g_free ($1);
723                                               }
724         |       link PARSE_URL                { GstElement *element =
725                                                           gst_element_make_from_uri (GST_URI_SINK, $2, NULL);
726                                                 if (!element) {
727                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, 
728                                                           _("no sink element for URI \"%s\""), $2);
729                                                   gst_parse_link_free ($1);
730                                                   g_free ($2);
731                                                   YYERROR;
732                                                 } else if ($1->sink_name || $1->sink_pads) {
733                                                   gst_object_unref (element);
734                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, 
735                                                           _("could not link sink element for URI \"%s\""), $2);
736                                                   gst_parse_link_free ($1);
737                                                   g_free ($2);
738                                                   YYERROR;
739                                                 } else {
740                                                   $$ = gst_parse_chain_new ();
741                                                   $$->first = $$->last = element;
742                                                   $$->front = $1;
743                                                   $$->front->sink = element;
744                                                   $$->elements = g_slist_prepend (NULL, element);
745                                                 }
746                                                 g_free ($2);
747                                               }
748         ;
749 graph:          /* NOP */                     { SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
750                                                 $$ = (graph_t *) graph;
751                                               }
752         |       chain                         { $$ = (graph_t *) graph;
753                                                 if ($1->front) {
754                                                   if (!$1->front->src_name) {
755                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
756                                                     gst_parse_free_link ($1->front);
757                                                   } else {
758                                                     $$->links = g_slist_prepend ($$->links, $1->front);
759                                                   }
760                                                   $1->front = NULL;
761                                                 }
762                                                 if ($1->back) {
763                                                   if (!$1->back->sink_name) {
764                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
765                                                     gst_parse_free_link ($1->back);
766                                                   } else {
767                                                     $$->links = g_slist_prepend ($$->links, $1->back);
768                                                   }
769                                                   $1->back = NULL;
770                                                 }
771                                                 $$->chain = $1;
772                                               }
773         ;
774
775 %%
776
777
778 static int
779 yyerror (void *scanner, graph_t *graph, const char *s)
780 {
781   /* FIXME: This should go into the GError somehow, but how? */
782   GST_WARNING ("Error during parsing: %s", s);
783   return -1;
784 }
785
786
787 GstElement *
788 _gst_parse_launch (const gchar *str, GError **error)
789 {
790   graph_t g;
791   gchar *dstr;
792   GSList *walk;
793   GstBin *bin = NULL;
794   GstElement *ret;
795   yyscan_t scanner;
796
797   g_return_val_if_fail (str != NULL, NULL);
798
799   g.chain = NULL;
800   g.links = NULL;
801   g.error = error;
802   
803 #ifdef __GST_PARSE_TRACE
804   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
805   __strings = __chains = __links = 0;
806 #endif /* __GST_PARSE_TRACE */
807
808   dstr = g_strdup (str);
809   _gst_parse_yylex_init (&scanner);
810   _gst_parse_yy_scan_string (dstr, scanner);
811
812 #ifndef YYDEBUG
813   yydebug = 1;
814 #endif
815
816   if (yyparse (scanner, &g) != 0) {
817     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
818         "Unrecoverable syntax error while parsing pipeline %s", str);
819     
820     _gst_parse_yylex_destroy (scanner);
821     g_free (dstr);
822   
823     goto error1;
824   }
825   _gst_parse_yylex_destroy (scanner);
826   g_free (dstr);
827   
828   GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
829       g.chain ? g_slist_length (g.chain->elements) : 0,
830       g_slist_length (g.links));
831   
832   if (!g.chain) {
833     ret = NULL;
834   } else if (!(((chain_t *) g.chain)->elements->next)) {
835     /* only one toplevel element */  
836     ret = (GstElement *) ((chain_t *) g.chain)->elements->data;
837     g_slist_free (((chain_t *) g.chain)->elements);
838     if (GST_IS_BIN (ret))
839       bin = GST_BIN (ret);
840     gst_parse_chain_free (g.chain);
841   } else {  
842     /* put all elements in our bin */
843     bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
844     g_assert (bin);
845     
846     for (walk = g.chain->elements; walk; walk = walk->next) {
847       if (walk->data != NULL)
848         gst_bin_add (bin, GST_ELEMENT (walk->data));
849     }
850     
851     g_slist_free (g.chain->elements);
852     ret = GST_ELEMENT (bin);
853     gst_parse_chain_free (g.chain);
854   }
855   
856   /* remove links */
857   for (walk = g.links; walk; walk = walk->next) {
858     link_t *l = (link_t *) walk->data;
859     if (!l->src) {
860       if (l->src_name) {
861         if (bin) {
862           l->src = gst_bin_get_by_name_recurse_up (bin, l->src_name);
863           if (l->src)
864             gst_object_unref (l->src);
865         } else {
866           l->src = strcmp (GST_ELEMENT_NAME (ret), l->src_name) == 0 ? ret : NULL;
867         }
868       }
869       if (!l->src) {
870         SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
871             "No element named \"%s\" - omitting link", l->src_name);
872         gst_parse_free_link (l);
873         continue;
874       }
875     }
876     if (!l->sink) {
877       if (l->sink_name) {
878         if (bin) {
879           l->sink = gst_bin_get_by_name_recurse_up (bin, l->sink_name);
880           if (l->sink)
881             gst_object_unref (l->sink);
882         } else {
883           l->sink = strcmp (GST_ELEMENT_NAME (ret), l->sink_name) == 0 ? ret : NULL;
884         }
885       }
886       if (!l->sink) {
887         SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
888             "No element named \"%s\" - omitting link", l->sink_name);
889         gst_parse_free_link (l);
890         continue;
891       }
892     }
893     gst_parse_perform_link (l, &g);
894   }
895   g_slist_free (g.links);
896
897 out:
898 #ifdef __GST_PARSE_TRACE
899   GST_CAT_DEBUG (GST_CAT_PIPELINE,
900       "TRACE: %u strings, %u chains and %u links left", __strings, __chains,
901       __links);
902   if (__strings || __chains || __links) {
903     g_warning ("TRACE: %u strings, %u chains and %u links left", __strings,
904         __chains, __links);
905   }
906 #endif /* __GST_PARSE_TRACE */
907
908   return ret;
909   
910 error1:
911   if (g.chain) {
912     g_slist_foreach (g.chain->elements, (GFunc)gst_object_unref, NULL);
913     g_slist_free (g.chain->elements);
914     gst_parse_chain_free (g.chain);
915   }
916
917   g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
918   g_slist_free (g.links);
919   
920   if (error)
921     g_assert (*error);
922   ret = NULL;
923   
924   goto out;
925 }