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