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