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