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