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