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