add extra error for empty pipeline
[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 #include "../gstparse.h"
8 #include "../gstinfo.h"
9 #include "types.h"
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #define YYERROR_VERBOSE 1
16 #define YYPARSE_PARAM graph
17
18 #ifdef __GST_PARSE_TRACE
19 static uint __strings;
20 static uint __links;
21 static uint __chains;
22 gchar *
23 __gst_parse_strdup (gchar *org)
24 {
25   __strings++;
26   gchar *ret;
27   ret = g_strdup (org);
28   /* g_print ("ALLOCATED STR   (%3u): %p %s\n", __strings, ret, ret); */
29   return ret;
30 }
31 void
32 __gst_parse_strfree (gchar *str)
33 {
34   if (str) {
35     /* g_print ("FREEING STR     (%3u): %p %s\n", __strings - 1, str, str); */
36     g_free (str);
37     g_return_if_fail (__strings > 0);
38     __strings--;
39   }
40 }
41 link_t *__gst_parse_link_new ()
42 {
43   link_t *ret;
44   __links++;
45   ret = g_new0 (link_t, 1);
46   /* g_print ("ALLOCATED LINK  (%3u): %p\n", __links, ret); */
47   return ret;
48 }
49 void
50 __gst_parse_link_free (link_t *data)
51 {
52   if (data) {
53     /* g_print ("FREEING LINK    (%3u): %p\n", __links - 1, data); */
54     g_free (data);
55     g_return_if_fail (__links > 0);
56     __links--;
57   }
58 }
59 chain_t *
60 __gst_parse_chain_new ()
61 {
62   chain_t *ret;
63   __chains++;
64   ret = g_new0 (chain_t, 1);
65   /* g_print ("ALLOCATED CHAIN (%3u): %p\n", __chains, ret); */
66   return ret;
67 }
68 void
69 __gst_parse_chain_free (chain_t *data)
70 {
71   if (data) {
72     /* g_print ("FREEING CHAIN   (%3u): %p\n", __chains - 1, data); */
73     g_free (data);
74     g_return_if_fail (__chains > 0);
75     __chains--;
76   }
77 }
78
79 #endif /* __GST_PARSE_TRACE */
80
81 typedef struct {
82   gchar *src_pad;
83   gchar *sink_pad;
84   GstElement *sink;
85   GstCaps *caps;
86   gulong signal_id;
87   /* FIXME: need to connect to "disposed" signal to clean up, but there is no such signal */
88 } DelayedLink;
89
90 #ifdef G_HAVE_ISO_VARARGS
91 #define SET_ERROR(error, type, ...) G_STMT_START{ \
92   if (error) { \
93     if (*(error)) { \
94       g_warning (__VA_ARGS__); \
95     } else { \
96       g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
97     }\
98   } \
99 }G_STMT_END
100 #define ERROR(type, ...) SET_ERROR (((graph_t *) graph)->error, (type), __VA_ARGS__ )
101 #ifdef GST_DEBUG_ENABLED
102 #  define YYDEBUG 1
103    /* bison 1.35 calls this macro with side effects, we need to make sure the
104       side effects work - crappy bison
105 #  define YYFPRINTF(a, ...) GST_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__)
106  */
107 #  define YYFPRINTF(a, ...) G_STMT_START{ \
108      gchar *temp = g_strdup_printf (__VA_ARGS__); \
109      GST_DEBUG (GST_CAT_PIPELINE, temp); \
110      g_free (temp); \
111    }G_STMT_END
112 #endif
113
114 #elif defined(G_HAVE_GNUC_VARARGS)
115
116 #define SET_ERROR(error, type, args...) G_STMT_START{ \
117   if (error) { \
118     if (*(error)) { \
119       g_warning ( ## args ); \
120     } else { \
121       g_set_error ((error), GST_PARSE_ERROR, (type), ## args ); \
122     }\
123   } \
124 }G_STMT_END
125 #define ERROR(type, args...) SET_ERROR (((graph_t *) graph)->error, (type), ## args )
126 #ifdef GST_DEBUG_ENABLED
127 #  define YYDEBUG 1
128    /* bison 1.35 calls this macro with side effects, we need to make sure the
129       side effects work - crappy bison
130 #  define YYFPRINTF(a, args...) GST_DEBUG (GST_CAT_PIPELINE, ## args )
131  */
132 #  define YYFPRINTF(a, args...) G_STMT_START{ \
133      gchar *temp = g_strdup_printf ( ## args ); \
134      GST_DEBUG (GST_CAT_PIPELINE, temp); \
135      g_free (temp); \
136    }G_STMT_END
137 #endif
138
139 #else
140
141 #define SET_ERROR(error, type, ...) G_STMT_START{ \
142   if (error) { \
143     if (*(error)) { \
144       g_warning ("error while parsing"); \
145     } else { \
146       g_set_error ((error), GST_PARSE_ERROR, (type), "error while parsing"); \
147     }\
148   } \
149 }G_STMT_END
150 #define ERROR(type, ...) SET_ERROR (((graph_t *) graph)->error, (type), "error while parsing")
151 #ifdef GST_DEBUG_ENABLED
152 #  define YYDEBUG 1
153 #endif
154
155 #endif /* G_HAVE_ISO_VARARGS */
156
157 #define GST_BIN_MAKE(res, type, chainval, assign) G_STMT_START{ \
158   chain_t *chain = chainval; \
159   GSList *walk; \
160   GstBin *bin = (GstBin *) gst_element_factory_make (type, NULL); \
161   if (!chain) { \
162     ERROR (GST_PARSE_ERROR_EMPTY_BIN, "Specified empty bin \"%s\", not allowed", type); \
163     g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
164     g_slist_free (assign); \
165     YYERROR; \
166   } else if (!bin) { \
167     ERROR (GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No bin \"%s\", omitting...", type); \
168     g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
169     g_slist_free (assign); \
170     res = chain; \
171   } else { \
172     walk = chain->elements; \
173     while (walk) { \
174       gst_bin_add (bin, GST_ELEMENT (walk->data)); \
175       walk = walk->next; \
176     } \
177     g_slist_free (chain->elements); \
178     chain->elements = g_slist_prepend (NULL, bin); \
179     res = chain; \
180     /* set the properties now */ \
181     walk = assign; \
182     while (walk) { \
183       gst_parse_element_set ((gchar *) walk->data, GST_ELEMENT (bin), graph); \
184       walk = g_slist_next (walk); \
185     } \
186     g_slist_free (assign); \
187   } \
188 }G_STMT_END
189
190 #define MAKE_LINK(link, _src, _src_name, _src_pads, _sink, _sink_name, _sink_pads) G_STMT_START{ \
191   link = gst_parse_link_new (); \
192   link->src = _src; \
193   link->sink = _sink; \
194   link->src_name = _src_name; \
195   link->sink_name = _sink_name; \
196   link->src_pads = _src_pads; \
197   link->sink_pads = _sink_pads; \
198   link->caps = NULL; \
199 }G_STMT_END
200
201 #define MAKE_REF(link, _src, _pads) G_STMT_START{ \
202   gchar *padname = _src; \
203   GSList *pads = _pads; \
204   if (padname) { \
205     while (*padname != '.') padname++; \
206     *padname = '\0'; \
207     padname++; \
208     if (*padname != '\0') \
209       pads = g_slist_prepend (pads, gst_parse_strdup (padname)); \
210   } \
211   MAKE_LINK (link, NULL, _src, pads, NULL, NULL, NULL); \
212 }G_STMT_END
213
214 static inline void gst_parse_unescape (gchar *str)
215 {
216   gchar *walk;
217   
218   g_return_if_fail (str != NULL);
219   
220   walk = str;
221   
222   while (*walk) {
223     if (*walk == '\\')
224       walk++;
225     *str = *walk;
226     str++;
227     walk++;
228   }
229   *str = '\0';
230 }
231 static void
232 gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
233 {
234   GParamSpec *pspec;
235   gchar *pos = value;
236   /* parse the string, so the property name is null-terminated an pos points
237      to the beginning of the value */
238   while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++; 
239   if (*pos == '=') { 
240     *pos = '\0'; 
241   } else { 
242     *pos = '\0'; 
243     pos++;
244     while (g_ascii_isspace (*pos)) pos++; 
245   } 
246   pos++; 
247   while (g_ascii_isspace (*pos)) pos++; 
248   if (*pos == '"') {
249     pos++;
250     pos[strlen (pos) - 1] = '\0';
251   }
252   gst_parse_unescape (pos); 
253   if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value))) { 
254     GValue v = { 0, }; 
255     GValue v2 = { 0, };
256     g_value_init (&v, G_PARAM_SPEC_VALUE_TYPE(pspec)); 
257     switch (G_TYPE_FUNDAMENTAL (G_PARAM_SPEC_VALUE_TYPE (pspec))) {
258     case G_TYPE_STRING:
259       g_value_set_string (&v, pos);
260       break;      
261     case G_TYPE_BOOLEAN:
262       if (g_ascii_strcasecmp (pos, "true") && g_ascii_strcasecmp (pos, "yes") && g_ascii_strcasecmp (pos, "1")) {
263         g_value_set_boolean (&v, FALSE);
264       } else {
265         g_value_set_boolean (&v, TRUE);
266       }
267       break;
268     case G_TYPE_ENUM: {
269       GEnumValue *en;
270       gchar *endptr = NULL;
271       GEnumClass *klass = (GEnumClass *) g_type_class_peek (G_PARAM_SPEC_VALUE_TYPE (pspec));
272       if (klass == NULL) goto error;
273       if (!(en = g_enum_get_value_by_name (klass, pos))) {
274         if (!(en = g_enum_get_value_by_nick (klass, pos))) {
275           gint i = strtol (pos, &endptr, 0);
276           if (endptr && *endptr == '\0') {
277             en = g_enum_get_value (klass, i);
278           }
279         }
280       }
281       if (!en)
282         goto error;
283       g_value_set_enum (&v, en->value);
284       break;
285     }
286     case G_TYPE_INT:
287     case G_TYPE_LONG:
288     case G_TYPE_INT64:
289       g_value_init (&v2, G_TYPE_LONG); 
290       g_value_set_long (&v2, strtol (pos, NULL, 0));
291       if (!g_value_transform (&v2, &v)) goto error;
292       break;      
293     case G_TYPE_UINT:
294     case G_TYPE_ULONG:
295     case G_TYPE_UINT64:
296       g_value_init (&v2, G_TYPE_ULONG); 
297       g_value_set_ulong (&v2, strtoul (pos, NULL, 0));
298       if (!g_value_transform (&v2, &v)) goto error;
299       break;
300     case G_TYPE_FLOAT:
301     case G_TYPE_DOUBLE:
302       g_value_init (&v2, G_TYPE_DOUBLE); 
303       g_value_set_double (&v2, atof (pos));
304       if (!g_value_transform (&v2, &v)) goto error;
305       break;
306     default:
307       /* add more */
308       g_warning ("property \"%s\" in element %s cannot be set", value, GST_ELEMENT_NAME (element)); 
309       goto error;
310     }
311     g_object_set_property (G_OBJECT (element), value, &v); 
312   } else { 
313     ERROR (GST_PARSE_ERROR_NO_SUCH_PROPERTY, "No property \"%s\" in element \"%s\"", value, GST_ELEMENT_NAME (element)); 
314   }
315
316 out:
317   gst_parse_strfree (value);
318   return;
319   
320 error:
321   ERROR (GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY, "Could not set property \"%s\" in element \"%s\" to \"%s\"", value, GST_ELEMENT_NAME (element), pos); 
322   goto out;
323 }
324 static inline void
325 gst_parse_free_link (link_t *link)
326 {
327   gst_parse_strfree (link->src_name);
328   gst_parse_strfree (link->sink_name);
329   g_slist_foreach (link->src_pads, (GFunc) gst_parse_strfree, NULL);
330   g_slist_foreach (link->sink_pads, (GFunc) gst_parse_strfree, NULL);
331   g_slist_free (link->src_pads);
332   g_slist_free (link->sink_pads);
333   gst_caps_unref (link->caps);
334   gst_parse_link_free (link);  
335 }
336 static void
337 gst_parse_element_lock (GstElement *element, gboolean lock)
338 {
339   GstPad *pad;
340   GList *walk = (GList *) gst_element_get_pad_list (element);
341   gboolean unlocked_peer = FALSE;
342   
343   if (gst_element_is_locked_state (element) == lock)
344     return;
345   /* check if we have an unlocked peer */
346   while (walk) {
347     pad = (GstPad *) GST_PAD_REALIZE (walk->data);
348     walk = walk->next;
349     if (GST_PAD_IS_SINK (pad) && GST_PAD_PEER (pad) &&
350         !gst_element_is_locked_state (GST_PAD_PARENT (GST_PAD_PEER (pad)))) {
351       unlocked_peer = TRUE;
352       break;
353     }
354   }  
355   
356   if (!(lock && unlocked_peer)) {
357     gst_element_set_locked_state (element, lock);
358     if (!lock)
359       gst_element_sync_state_with_parent (element);
360   } else {
361     return;
362   }
363   
364   /* check if there are other pads to (un)lock */
365   walk = (GList *) gst_element_get_pad_list (element);
366   while (walk) {
367     pad = (GstPad *) GST_PAD_REALIZE (walk->data);
368     walk = walk->next;
369     if (GST_PAD_IS_SRC (pad) && GST_PAD_PEER (pad)) {
370       GstElement *next = GST_ELEMENT (GST_OBJECT_PARENT (GST_PAD_PEER (pad)));
371       if (gst_element_is_locked_state (next) != lock)
372         gst_parse_element_lock (next, lock);
373     }
374   }
375 }
376 static void
377 gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
378 {
379   DelayedLink *link = (DelayedLink *) data;
380   
381   GST_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s", 
382             GST_ELEMENT_NAME (src), link->src_pad,
383             GST_ELEMENT_NAME (link->sink), link->sink_pad);
384
385   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink, link->sink_pad, link->caps)) {
386     /* do this here, we don't want to get any problems later on when unlocking states */
387     GST_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked", 
388                GST_ELEMENT_NAME (src), link->src_pad,
389                GST_ELEMENT_NAME (link->sink), link->sink_pad);
390     g_signal_handler_disconnect (src, link->signal_id);
391     g_free (link->src_pad);
392     g_free (link->sink_pad);
393     gst_caps_unref (link->caps);
394     if (!gst_element_is_locked_state (src))
395       gst_parse_element_lock (link->sink, FALSE);
396     g_free (link);
397   }
398 }
399 /* both padnames and the caps may be NULL */
400 static gboolean
401 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad, 
402                                 GstElement *sink, const gchar *sink_pad, GstCaps *caps)
403 {
404   GList *templs = gst_element_get_pad_template_list (src);
405          
406   while (templs) {
407     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
408     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) && (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
409     {
410       /* TODO: maybe we should check if src_pad matches this template's names */
411
412       GST_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s", 
413                  GST_ELEMENT_NAME (src), src_pad, GST_ELEMENT_NAME (sink), sink_pad);
414
415       DelayedLink *data = g_new (DelayedLink, 1);
416       data->src_pad = g_strdup (src_pad);
417       data->sink = sink;
418       data->sink_pad = g_strdup (sink_pad);
419       data->caps = gst_caps_ref (caps);
420       data->signal_id = g_signal_connect (G_OBJECT (src), "new_pad", 
421                                           G_CALLBACK (gst_parse_found_pad), data);
422       return TRUE;
423     }
424     templs = g_list_next (templs);
425   }
426   return FALSE;
427 }
428 /**
429  * performs a link and frees the struct. src and sink elements must be given
430  * return values:  0 - link performed
431  *                 1 - link delayed
432  *                <0 - error
433  */
434 static gint
435 gst_parse_perform_link (link_t *link, graph_t *graph)
436 {
437   GstElement *src = link->src;
438   GstElement *sink = link->sink;
439   GSList *srcs = link->src_pads;
440   GSList *sinks = link->sink_pads;
441   g_assert (GST_IS_ELEMENT (src));
442   g_assert (GST_IS_ELEMENT (sink));
443   
444   GST_INFO (GST_CAT_PIPELINE, "linking %s(%s):%u to %s(%s):%u", 
445             GST_ELEMENT_NAME (src), link->src_name ? link->src_name : "---", g_slist_length (srcs),
446             GST_ELEMENT_NAME (sink), link->sink_name ? link->sink_name : "---", g_slist_length (sinks));
447
448   if (!srcs || !sinks) {
449     if (gst_element_link_pads_filtered (src, srcs ? (const gchar *) srcs->data : NULL,
450                                         sink, sinks ? (const gchar *) sinks->data : NULL,
451                                         link->caps)) {
452       gst_parse_element_lock (sink, gst_element_is_locked_state (src));
453       goto success;
454     } else {
455       if (gst_parse_perform_delayed_link (src, srcs ? (const gchar *) srcs->data : NULL,
456                                           sink, sinks ? (const gchar *) sinks->data : NULL,
457                                           link->caps)) {
458         gst_parse_element_lock (sink, TRUE);
459         goto success;
460       } else {
461         goto error;
462       }
463     }
464   }
465   if (g_slist_length (link->src_pads) != g_slist_length (link->src_pads)) {
466     goto error;
467   }
468   while (srcs && sinks) {
469     const gchar *src_pad = (const gchar *) srcs->data;
470     const gchar *sink_pad = (const gchar *) sinks->data;
471     srcs = g_slist_next (srcs);
472     sinks = g_slist_next (sinks);
473     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad, link->caps)) {
474       gst_parse_element_lock (sink, gst_element_is_locked_state (src));
475       continue;
476     } else {
477       if (gst_parse_perform_delayed_link (src, src_pad,
478                                           sink, sink_pad,
479                                           link->caps)) {
480         gst_parse_element_lock (sink, TRUE);
481         continue;
482       } else {
483         goto error;
484       }
485     }
486   }
487   
488 success:
489   gst_parse_free_link (link);
490   return 0;
491   
492 error:
493   ERROR (GST_PARSE_ERROR_LINK, "could not link %s to %s", GST_ELEMENT_NAME (src), GST_ELEMENT_NAME (sink));
494   gst_parse_free_link (link);
495   return -1;
496 }
497
498
499 static int yylex (void *lvalp);
500 static int yyerror (const char *s);
501 %}
502
503 %union {
504     gchar *s;
505     chain_t *c;
506     link_t *l;
507     GstElement *e;
508     GSList *p;
509     graph_t *g;
510 }
511
512 %token <s> IDENTIFIER
513 %left <s> REF PADREF BINREF
514 %token <s> ASSIGNMENT
515
516 %type <g> graph
517 %type <c> chain bin
518 %type <l> reference
519 %type <l> linkpart link
520 %type <p> linklist
521 %type <e> element
522 %type <p> padlist pads assignments
523
524 %left '{' '}' '(' ')'
525 %left ','
526 %right '.'
527 %left '!' '='
528
529 %pure_parser
530
531 %start graph
532 %%
533
534 element:        IDENTIFIER                    { $$ = gst_element_factory_make ($1, NULL); 
535                                                 if (!$$)
536                                                   ERROR (GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element \"%s\"", $1);
537                                                 gst_parse_strfree ($1);
538                                                 if (!$$)
539                                                   YYERROR;
540                                               }
541         |       element ASSIGNMENT            { gst_parse_element_set ($2, $1, graph);
542                                                 $$ = $1;
543                                               }
544         ;
545         
546 assignments:    /* NOP */                     { $$ = NULL; }
547         |       assignments ASSIGNMENT        { $$ = g_slist_prepend ($1, $2); }
548         ;               
549 bin:            '{' assignments chain '}'     { GST_BIN_MAKE ($$, "thread", $3, $2); }
550         |       '(' assignments chain ')'     { GST_BIN_MAKE ($$, "bin", $3, $2); }
551         |       BINREF assignments chain ')'  { GST_BIN_MAKE ($$, $1, $3, $2); 
552                                                 gst_parse_strfree ($1);
553                                               }
554         |       '{' assignments '}'           { GST_BIN_MAKE ($$, "thread", NULL, $2); }
555         |       '(' assignments '}'           { GST_BIN_MAKE ($$, "thread", NULL, $2); }
556         |       BINREF assignments ')'        { GST_BIN_MAKE ($$, $1, NULL, $2); 
557                                                 gst_parse_strfree ($1);
558                                               }
559         |       '{' assignments error '}'     { GST_BIN_MAKE ($$, "thread", NULL, $2); }
560         |       '(' assignments error '}'     { GST_BIN_MAKE ($$, "thread", NULL, $2); }
561         |       BINREF assignments error ')'  { GST_BIN_MAKE ($$, $1, NULL, $2); 
562                                                 gst_parse_strfree ($1);
563                                               }
564         ;
565         
566 pads:           PADREF                        { $$ = g_slist_prepend (NULL, $1); }
567         |       PADREF padlist                { $$ = $2;
568                                                 $$ = g_slist_prepend ($$, $1);
569                                               }                              
570         ;
571 padlist:        ',' IDENTIFIER                { $$ = g_slist_prepend (NULL, $2); }
572         |       ',' IDENTIFIER padlist        { $$ = g_slist_prepend ($3, $2); }
573         ;
574         
575 reference:      REF                           { MAKE_REF ($$, $1, NULL); }
576         |       REF padlist                   { MAKE_REF ($$, $1, $2); }
577         ;
578
579 linkpart:       reference                     { $$ = $1; }
580         |       pads                          { MAKE_REF ($$, NULL, $1); }
581         |       /* NOP */                     { MAKE_REF ($$, NULL, NULL); }
582         ;
583         
584 link:           linkpart '!' linkpart         { $$ = $1;
585                                                 $$->sink_name = $3->src_name;
586                                                 $$->sink_pads = $3->src_pads;
587                                                 gst_parse_link_free ($3);
588                                               }
589         ;
590         
591 linklist:       link                          { $$ = g_slist_prepend (NULL, $1); }
592         |       link linklist                 { $$ = g_slist_prepend ($2, $1); }
593         |       linklist error                { $$ = $1; }
594         ;       
595
596 chain:          element                       { $$ = gst_parse_chain_new ();
597                                                 $$->first = $$->last = $1;
598                                                 $$->front = $$->back = NULL;
599                                                 $$->elements = g_slist_prepend (NULL, $1);
600                                               }
601         |       bin                           { $$ = $1; }
602         |       chain chain                   { if ($1->back && $2->front) {
603                                                   if (!$1->back->sink_name) {
604                                                     ERROR (GST_PARSE_ERROR_LINK, "link without source element");
605                                                     gst_parse_free_link ($1->back);
606                                                   } else {
607                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
608                                                   }
609                                                   if (!$2->front->src_name) {
610                                                     ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
611                                                     gst_parse_free_link ($2->front);
612                                                   } else {
613                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
614                                                   }
615                                                   $1->back = NULL;
616                                                 } else if ($1->back) {
617                                                   if (!$1->back->sink_name) {
618                                                     $1->back->sink = $2->first;
619                                                   }
620                                                 } else if ($2->front) {
621                                                   if (!$2->front->src_name) {
622                                                     $2->front->src = $1->last;
623                                                   }
624                                                   $1->back = $2->front;
625                                                 }
626                                                 
627                                                 if ($1->back) {
628                                                   if (!$1->back->sink || !$1->back->src) {
629                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $1->back);
630                                                     $1->back = NULL;
631                                                   } else {
632                                                     gst_parse_perform_link ($1->back, (graph_t *) graph);
633                                                   }
634                                                 }
635                                                 $1->last = $2->last;
636                                                 $1->back = $2->back;
637                                                 $1->elements = g_slist_concat ($1->elements, $2->elements);
638                                                 gst_parse_chain_free ($2);
639                                                 $$ = $1;
640                                               }
641         |       link chain                    { if ($2->front) {
642                                                   if (!$2->front->src_name) {
643                                                     ERROR (GST_PARSE_ERROR_LINK, "link without source element");
644                                                     gst_parse_free_link ($2->front);
645                                                   } else {
646                                                     ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, $2->front);
647                                                   }
648                                                 }
649                                                 if (!$1->sink_name) {
650                                                   $1->sink = $2->first;
651                                                 }
652                                                 $2->front = $1;
653                                                 $$ = $2;
654                                               }
655                                         
656         |       chain linklist                { GSList *walk;
657                                                 if ($1->back) {
658                                                   $2 = g_slist_prepend ($2, $1->back);
659                                                   $1->back = NULL;
660                                                 } else {
661                                                   if (!((link_t *) $2->data)->src_name) {
662                                                     ((link_t *) $2->data)->src = $1->last;
663                                                   }                                               
664                                                 }
665                                                 walk = $2;
666                                                 while (walk) {
667                                                   link_t *link = (link_t *) walk->data;
668                                                   walk = walk->next;
669                                                   if (!link->sink_name && walk) {
670                                                     ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
671                                                     gst_parse_free_link (link);
672                                                   } else if (!link->src_name && !link->src) {
673                                                     ERROR (GST_PARSE_ERROR_LINK, "link without source element");
674                                                     gst_parse_free_link (link);
675                                                   } else {
676                                                     if (walk) {
677                                                       ((graph_t *) graph)->links = g_slist_prepend (((graph_t *) graph)->links, link);
678                                                     } else {
679                                                       $1->back = link;
680                                                     }
681                                                   }
682                                                 }
683                                                 g_slist_free ($2);
684                                                 $$ = $1;
685                                               }
686         |       chain error                   { $$ = $1; }
687         ;
688         
689 graph:          /* NOP */                     { ERROR (GST_PARSE_ERROR_EMPTY, "Empty pipeline not allowed");
690                                                 $$ = (graph_t *) graph;
691                                               }
692         |       chain                         { $$ = (graph_t *) graph;
693                                                 if ($1->front) {
694                                                   if (!$1->front->src_name) {
695                                                     ERROR (GST_PARSE_ERROR_LINK, "link without source element");
696                                                     gst_parse_free_link ($1->front);
697                                                   } else {
698                                                     $$->links = g_slist_prepend ($$->links, $1->front);
699                                                   }
700                                                   $1->front = NULL;
701                                                 }
702                                                 if ($1->back) {
703                                                   if (!$1->back->sink_name) {
704                                                     ERROR (GST_PARSE_ERROR_LINK, "link without sink element");
705                                                     gst_parse_free_link ($1->back);
706                                                   } else {
707                                                     $$->links = g_slist_prepend ($$->links, $1->back);
708                                                   }
709                                                   $1->back = NULL;
710                                                 }
711                                                 $$->chain = $1;
712                                               }
713         ;
714
715 %%
716
717 extern FILE *_gst_parse_yyin;
718 int _gst_parse_yylex (YYSTYPE *lvalp);
719
720 static int yylex (void *lvalp) {
721     return _gst_parse_yylex ((YYSTYPE*) lvalp);
722 }
723
724 static int
725 yyerror (const char *s)
726 {
727   /* FIXME: This should go into the GError somehow, but how? */
728   g_warning ("error: %s\n", s);
729   return -1;
730 }
731
732 int _gst_parse_yy_scan_string (char*);
733 GstElement *
734 _gst_parse_launch (const gchar *str, GError **error)
735 {
736   graph_t g;
737   gchar *dstr;
738   GSList *walk;
739   GstBin *bin = NULL;
740   GstElement *ret;
741   
742   g_return_val_if_fail (str != NULL, NULL);
743
744   g.chain = NULL;
745   g.links = NULL;
746   g.error = error;
747   
748 #ifdef __GST_PARSE_TRACE
749   GST_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
750   __strings = __chains = __links = 0;
751 #endif /* __GST_PARSE_TRACE */
752
753   dstr = g_strdup (str);
754   _gst_parse_yy_scan_string (dstr);
755
756 #ifdef GST_DEBUG_ENABLED
757   yydebug = 1;
758 #endif
759
760   if (yyparse (&g) != 0) {
761     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX, "Unrecoverable syntax error while parsing pipeline");
762     
763     goto error1;
764   }
765   g_free (dstr);
766   
767   GST_INFO (GST_CAT_PIPELINE, "got %u elements and %u links", g.chain ? g_slist_length (g.chain->elements) : 0, g_slist_length (g.links));
768   
769   if (!g.chain) {
770     ret = NULL;
771   } else if (!(((chain_t *) g.chain)->elements->next)) {
772     /* only one toplevel element */  
773     ret = (GstElement *) ((chain_t *) g.chain)->elements->data;
774     g_slist_free (((chain_t *) g.chain)->elements);
775     if (GST_IS_BIN (ret))
776       bin = GST_BIN (ret);
777   } else {  
778     /* put all elements in our bin */
779     bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
780     g_assert (bin);
781     walk = g.chain->elements;
782     while (walk) {
783       gst_bin_add (bin, GST_ELEMENT (walk->data));
784       walk = g_slist_next (walk);  
785     }
786     g_slist_free (g.chain->elements);
787     ret = GST_ELEMENT (bin);
788   }
789   gst_parse_chain_free (g.chain);
790   
791   /* remove links */
792   walk = g.links;
793   while (walk) {
794     link_t *l = (link_t *) walk->data;
795     GstElement *sink;
796     walk = g_slist_next (walk);
797     if (!l->src) {
798       if (l->src_name) {
799         if (bin) {
800           l->src = gst_bin_get_by_name_recurse_up (bin, l->src_name);
801         } else {
802           l->src = strcmp (GST_ELEMENT_NAME (ret), l->src_name) == 0 ? ret : NULL;
803         }
804       }
805       if (!l->src) {
806         SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element named \"%s\" - omitting link", l->src_name);
807         gst_parse_free_link (l);
808         continue;
809       }
810     }
811     if (!l->sink) {
812       if (l->sink_name) {
813         if (bin) {
814           l->sink = gst_bin_get_by_name_recurse_up (bin, l->sink_name);
815         } else {
816           l->sink = strcmp (GST_ELEMENT_NAME (ret), l->sink_name) == 0 ? ret : NULL;
817         }
818       }
819       if (!l->sink) {
820         SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, "No element named \"%s\" - omitting link", l->sink_name);
821         gst_parse_free_link (l);
822         continue;
823       }
824     }
825     sink = l->sink;
826     gst_parse_perform_link (l, &g);
827   }
828   g_slist_free (g.links);
829
830 out:
831 #ifdef __GST_PARSE_TRACE
832   GST_DEBUG (GST_CAT_PIPELINE, "TRACE: %u strings, %u chains and %u links left", __strings, __chains, __links);
833   if (__strings || __chains || __links) {
834     g_warning ("TRACE: %u strings, %u chains and %u links left", __strings, __chains, __links);
835   }
836 #endif /* __GST_PARSE_TRACE */
837
838   return ret;
839   
840 error1:
841   g_free (dstr);
842   
843   if (g.chain) {
844     walk = g.chain->elements;
845     while (walk) {
846       gst_object_unref (GST_OBJECT (walk->data));
847       walk = walk->next;
848     }
849     g_slist_free (g.chain->elements);
850   }
851   gst_parse_chain_free (g.chain);
852   
853   walk = g.links;
854   while (walk) {
855     gst_parse_free_link ((link_t *) walk->data);
856     walk = walk->next;
857   }
858   g_slist_free (g.links);
859   
860   g_assert (*error);
861   ret = NULL;
862   
863   goto out;
864 }