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