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