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