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