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