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