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