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