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