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