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