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