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