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