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