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