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