gst/parse/: Reverted previous patch as it required to bump the flex dependency to...
[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_element_lock (GstElement *element, gboolean lock)
325 {
326   GstPad *pad;
327   GstIterator *pads;
328   gboolean unlocked_peer = FALSE;
329   gboolean done = FALSE;
330   GList *walk;
331   
332   if (gst_element_is_locked_state (element) == lock)
333     return;
334
335   return;
336
337   /* check if we have an unlocked peer */
338   pads = gst_element_iterate_pads (element);
339   while (!done) {
340     gpointer data;
341     switch (gst_iterator_next (pads, &data)) {
342       case GST_ITERATOR_OK:
343       {
344         GstPad *pad = GST_PAD_CAST (data);
345
346         if (GST_PAD_IS_SINK (pad) && GST_PAD_PEER (pad) &&
347             !gst_element_is_locked_state (
348                 GST_PAD_PARENT (GST_PAD_PEER (pad)))) {
349           unlocked_peer = TRUE;
350           done = TRUE;
351         }
352         gst_object_unref (GST_OBJECT (pad));
353         break;
354       }
355       case GST_ITERATOR_RESYNC:
356         unlocked_peer = FALSE;
357         gst_iterator_resync (pads);
358         break;
359       case GST_ITERATOR_DONE:
360         done = TRUE;
361         break;
362       default:
363         g_assert_not_reached ();
364         break;
365     }
366   }
367   gst_iterator_free (pads);
368
369   if (!(lock && unlocked_peer)) {
370     GST_CAT_DEBUG (GST_CAT_PIPELINE, "setting locked state on element");
371     gst_element_set_locked_state (element, lock);
372     if (!lock)
373     {
374       /* try to sync state with parent */
375       GST_CAT_DEBUG (GST_CAT_PIPELINE,
376           "trying to sync state of element with parent");
377       /* FIXME: it would be nice if we can figure out why it failed
378          (e.g. caps nego) and give an error about that instead. */
379       if (gst_element_set_state (element, GST_STATE_PLAYING)
380           == GST_STATE_CHANGE_FAILURE)
381         GST_ELEMENT_ERROR (element, CORE, STATE_CHANGE, (NULL), (NULL));
382     }
383   } else {
384     return;
385   }
386   
387   /* check if there are other pads to (un)lock */
388   walk = (GList *) element->pads;
389   for  (; walk; walk = walk->next) {
390     pad = GST_PAD_CAST (walk->data);
391     if (GST_PAD_IS_SRC (pad) && GST_PAD_PEER (pad)) {
392       GstElement *next = GST_ELEMENT (GST_OBJECT_PARENT (GST_PAD_PEER (pad)));
393       if (gst_element_is_locked_state (next) != lock)
394         gst_parse_element_lock (next, lock);
395     }
396   }
397 }
398 static void
399 gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
400 {
401   DelayedLink *link = (DelayedLink *) data;
402   
403   GST_CAT_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s", 
404                 GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
405                 GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
406
407   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
408       link->sink_pad, link->caps)) {
409     /* do this here, we don't want to get any problems later on when
410      * unlocking states */
411     GST_CAT_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked", 
412                    GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
413                    GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
414     g_signal_handler_disconnect (src, link->signal_id);
415     g_free (link->src_pad);
416     g_free (link->sink_pad);
417     if (link->caps) gst_caps_unref (link->caps);
418     if (!gst_element_is_locked_state (src))
419       gst_parse_element_lock (link->sink, FALSE);
420     g_free (link);
421   }
422 }
423 /* both padnames and the caps may be NULL */
424 static gboolean
425 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad, 
426                                 GstElement *sink, const gchar *sink_pad,
427                                 GstCaps *caps)
428 {
429   GList *templs = gst_element_class_get_pad_template_list (
430       GST_ELEMENT_GET_CLASS (src));
431          
432   for (; templs; templs = templs->next) {
433     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
434     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
435         (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
436     {
437       DelayedLink *data = g_new (DelayedLink, 1); 
438       
439       /* TODO: maybe we should check if src_pad matches this template's names */
440
441       GST_CAT_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s", 
442                      GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (src_pad),
443                      GST_STR_NULL (GST_ELEMENT_NAME (sink)), GST_STR_NULL (sink_pad));
444
445       data->src_pad = g_strdup (src_pad);
446       data->sink = sink;
447       data->sink_pad = g_strdup (sink_pad);
448       if (caps) {
449         data->caps = gst_caps_copy (caps);
450       } else {
451         data->caps = NULL;
452       }
453       data->signal_id = g_signal_connect (G_OBJECT (src), "pad-added", 
454           G_CALLBACK (gst_parse_found_pad), data);
455       return TRUE;
456     }
457   }
458   return FALSE;
459 }
460 /*
461  * performs a link and frees the struct. src and sink elements must be given
462  * return values   0 - link performed
463  *                 1 - link delayed
464  *                <0 - error
465  */
466 static gint
467 gst_parse_perform_link (link_t *link, graph_t *graph)
468 {
469   GstElement *src = link->src;
470   GstElement *sink = link->sink;
471   GSList *srcs = link->src_pads;
472   GSList *sinks = link->sink_pads;
473   g_assert (GST_IS_ELEMENT (src));
474   g_assert (GST_IS_ELEMENT (sink));
475   
476   GST_CAT_INFO (GST_CAT_PIPELINE,
477       "linking %s:%s to %s:%s (%u/%u) with caps \"%" GST_PTR_FORMAT "\"", 
478       GST_ELEMENT_NAME (src), link->src_name ? link->src_name : "(any)",
479       GST_ELEMENT_NAME (sink), link->sink_name ? link->sink_name : "(any)",
480       g_slist_length (srcs), g_slist_length (sinks), link->caps);
481
482   if (!srcs || !sinks) {
483     if (gst_element_link_pads_filtered (src,
484         srcs ? (const gchar *) srcs->data : NULL, sink,
485         sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
486       gst_parse_element_lock (sink, gst_element_is_locked_state (src));
487       goto success;
488     } else {
489       if (gst_parse_perform_delayed_link (src,
490           srcs ? (const gchar *) srcs->data : NULL,
491           sink, sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
492         gst_parse_element_lock (sink, TRUE);
493         goto success;
494       } else {
495         goto error;
496       }
497     }
498   }
499   if (g_slist_length (link->src_pads) != g_slist_length (link->src_pads)) {
500     goto error;
501   }
502   while (srcs && sinks) {
503     const gchar *src_pad = (const gchar *) srcs->data;
504     const gchar *sink_pad = (const gchar *) sinks->data;
505     srcs = g_slist_next (srcs);
506     sinks = g_slist_next (sinks);
507     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
508         link->caps)) {
509       gst_parse_element_lock (sink, gst_element_is_locked_state (src));
510       continue;
511     } else {
512       if (gst_parse_perform_delayed_link (src, src_pad,
513                                           sink, sink_pad,
514                                           link->caps)) {
515         gst_parse_element_lock (sink, TRUE);
516         continue;
517       } else {
518         goto error;
519       }
520     }
521   }
522   
523 success:
524   gst_parse_free_link (link);
525   return 0;
526   
527 error:
528   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK,
529       _("could not link %s to %s"), GST_ELEMENT_NAME (src),
530       GST_ELEMENT_NAME (sink));
531   gst_parse_free_link (link);
532   return -1;
533 }
534
535
536 static int yylex (void *lvalp);
537 static int yyerror (const char *s);
538 %}
539
540 %union {
541     gchar *s;
542     chain_t *c;
543     link_t *l;
544     GstElement *e;
545     GSList *p;
546     graph_t *g;
547 }
548
549 %token <s> PARSE_URL
550 %token <s> IDENTIFIER
551 %left <s> REF PADREF BINREF
552 %token <s> ASSIGNMENT
553 %token <s> LINK
554
555 %type <g> graph
556 %type <c> chain bin
557 %type <l> reference
558 %type <l> linkpart link
559 %type <p> linklist
560 %type <e> element 
561 %type <p> padlist pads assignments
562
563 %left '(' ')'
564 %left ','
565 %right '.'
566 %left '!' '='
567
568 %pure_parser
569
570 %start graph
571 %%
572
573 element:        IDENTIFIER                    { $$ = gst_element_factory_make ($1, NULL); 
574                                                 if ($$ == NULL) {
575                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), $1);
576                                                   gst_parse_strfree ($1);
577                                                   YYERROR;
578                                                 }
579                                                 gst_parse_strfree ($1);
580                                               }
581         |       element ASSIGNMENT            { gst_parse_element_set ($2, $1, graph);
582                                                 $$ = $1;
583                                               }
584         ;
585 assignments:    /* NOP */                     { $$ = NULL; }
586         |       assignments ASSIGNMENT        { $$ = g_slist_prepend ($1, $2); }
587         ;               
588 bin:            '(' assignments chain ')' { GST_BIN_MAKE ($$, "bin", $3, $2); }
589         |       BINREF assignments chain ')'  { GST_BIN_MAKE ($$, $1, $3, $2); 
590                                                 gst_parse_strfree ($1);
591                                               }
592         |       BINREF assignments ')'        { GST_BIN_MAKE ($$, $1, NULL, $2); 
593                                                 gst_parse_strfree ($1);
594                                               }
595         |       BINREF assignments error ')'  { GST_BIN_MAKE ($$, $1, NULL, $2); 
596                                                 gst_parse_strfree ($1);
597                                               }
598         ;
599         
600 pads:           PADREF                        { $$ = g_slist_prepend (NULL, $1); }
601         |       PADREF padlist                { $$ = $2;
602                                                 $$ = g_slist_prepend ($$, $1);
603                                               }                              
604         ;
605 padlist:        ',' IDENTIFIER                { $$ = g_slist_prepend (NULL, $2); }
606         |       ',' IDENTIFIER padlist        { $$ = g_slist_prepend ($3, $2); }
607         ;
608         
609 reference:      REF                           { MAKE_REF ($$, $1, NULL); }
610         |       REF padlist                   { MAKE_REF ($$, $1, $2); }
611         ;
612
613 linkpart:       reference                     { $$ = $1; }
614         |       pads                          { MAKE_REF ($$, NULL, $1); }
615         |       /* NOP */                     { MAKE_REF ($$, NULL, NULL); }
616         ;
617         
618 link:           linkpart LINK linkpart        { $$ = $1;
619                                                 if ($2) {
620                                                   $$->caps = gst_caps_from_string ($2);
621                                                   if ($$->caps == NULL)
622                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $2);
623                                                   gst_parse_strfree ($2);
624                                                 }
625                                                 $$->sink_name = $3->src_name;
626                                                 $$->sink_pads = $3->src_pads;
627                                                 gst_parse_link_free ($3);
628                                               }
629         ;
630         
631 linklist:       link                          { $$ = g_slist_prepend (NULL, $1); }
632         |       link linklist                 { $$ = g_slist_prepend ($2, $1); }
633         |       linklist error                { $$ = $1; }
634         ;       
635
636 chain:          element                       { $$ = gst_parse_chain_new ();
637                                                 $$->first = $$->last = $1;
638                                                 $$->front = $$->back = NULL;
639                                                 $$->elements = g_slist_prepend (NULL, $1);
640                                               }
641         |       bin                           { $$ = $1; }
642         |       chain chain                   { if ($1->back && $2->front) {
643                                                   if (!$1->back->sink_name) {
644                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
645                                                     gst_parse_free_link ($1->back);
646                                                   } else {
647                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
648                                                   }
649                                                   if (!$2->front->src_name) {
650                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
651                                                     gst_parse_free_link ($2->front);
652                                                   } else {
653                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
654                                                   }
655                                                   $1->back = NULL;
656                                                 } else if ($1->back) {
657                                                   if (!$1->back->sink_name) {
658                                                     $1->back->sink = $2->first;
659                                                   }
660                                                 } else if ($2->front) {
661                                                   if (!$2->front->src_name) {
662                                                     $2->front->src = $1->last;
663                                                   }
664                                                   $1->back = $2->front;
665                                                 }
666                                                 
667                                                 if ($1->back) {
668                                                   ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
669                                                 }
670                                                 $1->last = $2->last;
671                                                 $1->back = $2->back;
672                                                 $1->elements = g_slist_concat ($1->elements, $2->elements);
673                                                 if ($2)
674                                                   gst_parse_chain_free ($2);
675                                                 $$ = $1;
676                                               }
677         |       chain linklist                { GSList *walk;
678                                                 if ($1->back) {
679                                                   $2 = g_slist_prepend ($2, $1->back);
680                                                   $1->back = NULL;
681                                                 } else {
682                                                   if (!((link_t *) $2->data)->src_name) {
683                                                     ((link_t *) $2->data)->src = $1->last;
684                                                   }                                               
685                                                 }
686                                                 for (walk = $2; walk; walk = walk->next) {
687                                                   link_t *link = (link_t *) walk->data;
688                                                   if (!link->sink_name && walk->next) {
689                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
690                                                     gst_parse_free_link (link);
691                                                   } else if (!link->src_name && !link->src) {
692                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
693                                                     gst_parse_free_link (link);
694                                                   } else {
695                                                     if (walk->next) {
696                                                       ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, link);
697                                                     } else {
698                                                       $1->back = link;
699                                                     }
700                                                   }
701                                                 }
702                                                 g_slist_free ($2);
703                                                 $$ = $1;
704                                               }
705         |       chain error                   { $$ = $1; }
706         |       link chain                    { if ($2->front) {
707                                                   if (!$2->front->src_name) {
708                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
709                                                     gst_parse_free_link ($2->front);
710                                                   } else {
711                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
712                                                   }
713                                                 }
714                                                 if (!$1->sink_name) {
715                                                   $1->sink = $2->first;
716                                                 }
717                                                 $2->front = $1;
718                                                 $$ = $2;
719                                               }
720         |       PARSE_URL chain               { $$ = $2;
721                                                 if ($$->front) {
722                                                   GstElement *element = 
723                                                           gst_element_make_from_uri (GST_URI_SRC, $1, NULL);
724                                                   if (!element) {
725                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, 
726                                                             _("no source element for URI \"%s\""), $1);
727                                                   } else {
728                                                     $$->front->src = element;
729                                                     ((graph_t *) graph)->links = g_slist_prepend (
730                                                             ((graph_t *) graph)->links, $$->front);
731                                                     $$->front = NULL;
732                                                     $$->elements = g_slist_prepend ($$->elements, element);
733                                                   }
734                                                 } else {
735                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, 
736                                                           _("no element to link URI \"%s\" to"), $1);
737                                                 }
738                                                 g_free ($1);
739                                               }
740         |       link PARSE_URL                { GstElement *element =
741                                                           gst_element_make_from_uri (GST_URI_SINK, $2, NULL);
742                                                 if (!element) {
743                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, 
744                                                           _("no sink element for URI \"%s\""), $2);
745                                                   gst_parse_link_free ($1);
746                                                   g_free ($2);
747                                                   YYERROR;
748                                                 } else if ($1->sink_name || $1->sink_pads) {
749                                                   gst_object_unref (element);
750                                                   SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, 
751                                                           _("could not link sink element for URI \"%s\""), $2);
752                                                   gst_parse_link_free ($1);
753                                                   g_free ($2);
754                                                   YYERROR;
755                                                 } else {
756                                                   $$ = gst_parse_chain_new ();
757                                                   $$->first = $$->last = element;
758                                                   $$->front = $1;
759                                                   $$->front->sink = element;
760                                                   $$->elements = g_slist_prepend (NULL, element);
761                                                 }
762                                                 g_free ($2);
763                                               }
764         ;
765 graph:          /* NOP */                     { SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
766                                                 $$ = (graph_t *) graph;
767                                               }
768         |       chain                         { $$ = (graph_t *) graph;
769                                                 if ($1->front) {
770                                                   if (!$1->front->src_name) {
771                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without source element"));
772                                                     gst_parse_free_link ($1->front);
773                                                   } else {
774                                                     $$->links = g_slist_prepend ($$->links, $1->front);
775                                                   }
776                                                   $1->front = NULL;
777                                                 }
778                                                 if ($1->back) {
779                                                   if (!$1->back->sink_name) {
780                                                     SET_ERROR (((graph_t *) graph)->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
781                                                     gst_parse_free_link ($1->back);
782                                                   } else {
783                                                     $$->links = g_slist_prepend ($$->links, $1->back);
784                                                   }
785                                                   $1->back = NULL;
786                                                 }
787                                                 $$->chain = $1;
788                                               }
789         ;
790
791 %%
792
793 extern FILE *_gst_parse_yyin;
794 int _gst_parse_yylex (YYSTYPE *lvalp);
795
796 static int yylex (void *lvalp) {
797     return _gst_parse_yylex ((YYSTYPE*) lvalp);
798 }
799
800 static int
801 yyerror (const char *s)
802 {
803   /* FIXME: This should go into the GError somehow, but how? */
804   GST_WARNING ("Error during parsing: %s", s);
805   return -1;
806 }
807
808 struct yy_buffer_state * _gst_parse_yy_scan_string (char*);
809 void _gst_parse_yy_delete_buffer (struct yy_buffer_state *);
810 GstElement *
811 _gst_parse_launch (const gchar *str, GError **error)
812 {
813   graph_t g;
814   gchar *dstr;
815   GSList *walk;
816   GstBin *bin = NULL;
817   GstElement *ret;
818   struct yy_buffer_state *buf;
819   
820   g_return_val_if_fail (str != NULL, NULL);
821
822   g.chain = NULL;
823   g.links = NULL;
824   g.error = error;
825   
826 #ifdef __GST_PARSE_TRACE
827   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
828   __strings = __chains = __links = 0;
829 #endif /* __GST_PARSE_TRACE */
830
831   dstr = g_strdup (str);
832   buf = _gst_parse_yy_scan_string (dstr);
833
834 #ifndef YYDEBUG
835   yydebug = 1;
836 #endif
837
838   if (yyparse (&g) != 0) {
839     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
840         "Unrecoverable syntax error while parsing pipeline %s", str);
841     
842     goto error1;
843   }
844   g_free (dstr);
845   _gst_parse_yy_delete_buffer (buf);
846   
847   GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
848       g.chain ? g_slist_length (g.chain->elements) : 0,
849       g_slist_length (g.links));
850   
851   if (!g.chain) {
852     ret = NULL;
853   } else if (!(((chain_t *) g.chain)->elements->next)) {
854     /* only one toplevel element */  
855     ret = (GstElement *) ((chain_t *) g.chain)->elements->data;
856     g_slist_free (((chain_t *) g.chain)->elements);
857     if (GST_IS_BIN (ret))
858       bin = GST_BIN (ret);
859     gst_parse_chain_free (g.chain);
860   } else {  
861     /* put all elements in our bin */
862     bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
863     g_assert (bin);
864     
865     for (walk = g.chain->elements; walk; walk = walk->next) {
866       if (walk->data != NULL)
867         gst_bin_add (bin, GST_ELEMENT (walk->data));
868     }
869     
870     g_slist_free (g.chain->elements);
871     ret = GST_ELEMENT (bin);
872     gst_parse_chain_free (g.chain);
873   }
874   
875   /* remove links */
876   for (walk = g.links; walk; walk = walk->next) {
877     link_t *l = (link_t *) walk->data;
878     if (!l->src) {
879       if (l->src_name) {
880         if (bin) {
881           l->src = gst_bin_get_by_name_recurse_up (bin, l->src_name);
882           if (l->src)
883             gst_object_unref (l->src);
884         } else {
885           l->src = strcmp (GST_ELEMENT_NAME (ret), l->src_name) == 0 ? ret : NULL;
886         }
887       }
888       if (!l->src) {
889         SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
890             "No element named \"%s\" - omitting link", l->src_name);
891         gst_parse_free_link (l);
892         continue;
893       }
894     }
895     if (!l->sink) {
896       if (l->sink_name) {
897         if (bin) {
898           l->sink = gst_bin_get_by_name_recurse_up (bin, l->sink_name);
899           if (l->sink)
900             gst_object_unref (l->sink);
901         } else {
902           l->sink = strcmp (GST_ELEMENT_NAME (ret), l->sink_name) == 0 ? ret : NULL;
903         }
904       }
905       if (!l->sink) {
906         SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
907             "No element named \"%s\" - omitting link", l->sink_name);
908         gst_parse_free_link (l);
909         continue;
910       }
911     }
912     gst_parse_perform_link (l, &g);
913   }
914   g_slist_free (g.links);
915
916 out:
917 #ifdef __GST_PARSE_TRACE
918   GST_CAT_DEBUG (GST_CAT_PIPELINE,
919       "TRACE: %u strings, %u chains and %u links left", __strings, __chains,
920       __links);
921   if (__strings || __chains || __links) {
922     g_warning ("TRACE: %u strings, %u chains and %u links left", __strings,
923         __chains, __links);
924   }
925 #endif /* __GST_PARSE_TRACE */
926
927   return ret;
928   
929 error1:
930   g_free (dstr);
931   
932   if (g.chain) {
933     g_slist_foreach (g.chain->elements, (GFunc)gst_object_unref, NULL);
934     g_slist_free (g.chain->elements);
935     gst_parse_chain_free (g.chain);
936   }
937
938   g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
939   g_slist_free (g.links);
940   
941   if (error)
942     g_assert (*error);
943   ret = NULL;
944   
945   goto out;
946 }