parse: make grammar.y work with Bison 3
[platform/upstream/gstreamer.git] / gst / parse / grammar.y
1 %{
2 #include "../gst_private.h"
3
4 #include <glib-object.h>
5 #include <glib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 #include "../gst-i18n-lib.h"
11
12 #include "../gstconfig.h"
13 #include "../gstparse.h"
14 #include "../gstinfo.h"
15 #include "../gsterror.h"
16 #include "../gststructure.h"
17 #include "../gsturi.h"
18 #include "../gstutils.h"
19 #include "../gstvalue.h"
20 #include "../gstchildproxy.h"
21 #include "types.h"
22
23 /* All error messages in this file are user-visible and need to be translated.
24  * Don't start the message with a capital, and don't end them with a period,
25  * as they will be presented inside a sentence/error.
26  */
27
28 #define YYERROR_VERBOSE 1
29
30 #define YYENABLE_NLS 0
31
32 #ifndef YYLTYPE_IS_TRIVIAL
33 #define YYLTYPE_IS_TRIVIAL 0
34 #endif
35
36 typedef void* yyscan_t;
37
38 int priv_gst_parse_yylex (void * yylval_param , yyscan_t yyscanner);
39 int priv_gst_parse_yylex_init (yyscan_t scanner);
40 int priv_gst_parse_yylex_destroy (yyscan_t scanner);
41 struct yy_buffer_state * priv_gst_parse_yy_scan_string (char* , yyscan_t);
42 void _gst_parse_yypush_buffer_state (void * new_buffer ,yyscan_t yyscanner );
43 void _gst_parse_yypop_buffer_state (yyscan_t yyscanner );
44
45 #ifdef __GST_PARSE_TRACE
46 static guint __strings;
47 static guint __links;
48 static guint __chains;
49 gchar *
50 __gst_parse_strdup (gchar *org)
51 {
52   gchar *ret;
53   __strings++;
54   ret = g_strdup (org);
55   /* g_print ("ALLOCATED STR   (%3u): %p %s\n", __strings, ret, ret); */
56   return ret;
57 }
58 void
59 __gst_parse_strfree (gchar *str)
60 {
61   if (str) {
62     /* g_print ("FREEING STR     (%3u): %p %s\n", __strings - 1, str, str); */
63     g_free (str);
64     g_return_if_fail (__strings > 0);
65     __strings--;
66   }
67 }
68 link_t *__gst_parse_link_new ()
69 {
70   link_t *ret;
71   __links++;
72   ret = g_slice_new0 (link_t);
73   /* g_print ("ALLOCATED LINK  (%3u): %p\n", __links, ret); */
74   return ret;
75 }
76 void
77 __gst_parse_link_free (link_t *data)
78 {
79   if (data) {
80     /* g_print ("FREEING LINK    (%3u): %p\n", __links - 1, data); */
81     g_slice_free (link_t, data);
82     g_return_if_fail (__links > 0);
83     __links--;
84   }
85 }
86 chain_t *
87 __gst_parse_chain_new ()
88 {
89   chain_t *ret;
90   __chains++;
91   ret = g_slice_new0 (chain_t);
92   /* g_print ("ALLOCATED CHAIN (%3u): %p\n", __chains, ret); */
93   return ret;
94 }
95 void
96 __gst_parse_chain_free (chain_t *data)
97 {
98   /* g_print ("FREEING CHAIN   (%3u): %p\n", __chains - 1, data); */
99   g_slice_free (chain_t, data);
100   g_return_if_fail (__chains > 0);
101   __chains--;
102 }
103
104 #endif /* __GST_PARSE_TRACE */
105
106 typedef struct {
107   gchar *src_pad;
108   gchar *sink_pad;
109   GstElement *sink;
110   GstCaps *caps;
111   gulong signal_id;
112 } DelayedLink;
113
114 typedef struct {
115   gchar *name;
116   gchar *value_str;
117   gulong signal_id;
118 } DelayedSet;
119
120 /*** define SET_ERROR macro/function */
121
122 #ifdef G_HAVE_ISO_VARARGS
123
124 #  define SET_ERROR(error, type, ...) \
125 G_STMT_START { \
126   GST_CAT_ERROR (GST_CAT_PIPELINE, __VA_ARGS__); \
127   if ((error) && !*(error)) { \
128     g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
129   } \
130 } G_STMT_END
131
132 #elif defined(G_HAVE_GNUC_VARARGS)
133
134 #  define SET_ERROR(error, type, args...) \
135 G_STMT_START { \
136   GST_CAT_ERROR (GST_CAT_PIPELINE, args ); \
137   if ((error) && !*(error)) { \
138     g_set_error ((error), GST_PARSE_ERROR, (type), args ); \
139   } \
140 } G_STMT_END
141
142 #else
143
144 static inline void
145 SET_ERROR (GError **error, gint type, const char *format, ...)
146 {
147   if (error) {
148     if (*error) {
149       g_warning ("error while parsing");
150     } else {
151       va_list varargs;
152       char *string;
153
154       va_start (varargs, format);
155       string = g_strdup_vprintf (format, varargs);
156       va_end (varargs);
157
158       g_set_error (error, GST_PARSE_ERROR, type, string);
159
160       g_free (string);
161     }
162   }
163 }
164
165 #endif /* G_HAVE_ISO_VARARGS */
166
167 /*** define YYPRINTF macro/function if we're debugging */
168
169 /* bison 1.35 calls this macro with side effects, we need to make sure the
170    side effects work - crappy bison */
171
172 #ifndef GST_DISABLE_GST_DEBUG
173 #  define YYDEBUG 1
174
175 #  ifdef G_HAVE_ISO_VARARGS
176
177 /* #  define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__) */
178 #    define YYFPRINTF(a, ...) \
179 G_STMT_START { \
180      GST_CAT_LOG (GST_CAT_PIPELINE, __VA_ARGS__); \
181 } G_STMT_END
182
183 #  elif defined(G_HAVE_GNUC_VARARGS)
184
185 #    define YYFPRINTF(a, args...) \
186 G_STMT_START { \
187      GST_CAT_LOG (GST_CAT_PIPELINE, args); \
188 } G_STMT_END
189
190 #  else
191
192 static inline void
193 YYPRINTF(const char *format, ...)
194 {
195   va_list varargs;
196   gchar *temp;
197
198   va_start (varargs, format);
199   temp = g_strdup_vprintf (format, varargs);
200   GST_CAT_LOG (GST_CAT_PIPELINE, "%s", temp);
201   g_free (temp);
202   va_end (varargs);
203 }
204
205 #  endif /* G_HAVE_ISO_VARARGS */
206
207 #endif /* GST_DISABLE_GST_DEBUG */
208
209 #define ADD_MISSING_ELEMENT(graph,name) G_STMT_START {                      \
210     if ((graph)->ctx) {                                                     \
211       (graph)->ctx->missing_elements =                                      \
212           g_list_append ((graph)->ctx->missing_elements, g_strdup (name));  \
213     } } G_STMT_END
214
215 static void
216 no_free (gconstpointer foo)
217 {
218   /* do nothing */
219 }
220
221 #define GST_BIN_MAKE(res, type, chainval, assign, type_string_free_func) \
222 G_STMT_START { \
223   chain_t *chain = chainval; \
224   GSList *walk; \
225   GstBin *bin = (GstBin *) gst_element_factory_make (type, NULL); \
226   if (!chain) { \
227     SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY_BIN, \
228         _("specified empty bin \"%s\", not allowed"), type); \
229     g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
230     g_slist_free (assign); \
231     gst_object_unref (bin); \
232     type_string_free_func (type); /* Need to clean up the string */ \
233     YYERROR; \
234   } else if (!bin) { \
235     ADD_MISSING_ELEMENT(graph, type); \
236     SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, \
237         _("no bin \"%s\", skipping"), type); \
238     g_slist_foreach (assign, (GFunc) gst_parse_strfree, NULL); \
239     g_slist_free (assign); \
240     res = chain; \
241   } else { \
242     for (walk = chain->elements; walk; walk = walk->next ) \
243       gst_bin_add (bin, GST_ELEMENT (walk->data)); \
244     g_slist_free (chain->elements); \
245     chain->elements = g_slist_prepend (NULL, bin); \
246     res = chain; \
247     /* set the properties now */ \
248     for (walk = assign; walk; walk = walk->next) \
249       gst_parse_element_set ((gchar *) walk->data, GST_ELEMENT (bin), graph); \
250     g_slist_free (assign); \
251   } \
252 } G_STMT_END
253
254 #define MAKE_LINK(link, _src, _src_name, _src_pads, _sink, _sink_name, _sink_pads) \
255 G_STMT_START { \
256   link = gst_parse_link_new (); \
257   link->src = _src; \
258   link->sink = _sink; \
259   link->src_name = _src_name; \
260   link->sink_name = _sink_name; \
261   link->src_pads = _src_pads; \
262   link->sink_pads = _sink_pads; \
263   link->caps = NULL; \
264 } G_STMT_END
265
266 #define MAKE_REF(link, _src, _pads) \
267 G_STMT_START { \
268   gchar *padname = _src; \
269   GSList *pads = _pads; \
270   if (padname) { \
271     while (*padname != '.') padname++; \
272     *padname = '\0'; \
273     padname++; \
274     if (*padname != '\0') \
275       pads = g_slist_prepend (pads, gst_parse_strdup (padname)); \
276   } \
277   MAKE_LINK (link, NULL, _src, pads, NULL, NULL, NULL); \
278 } G_STMT_END
279
280 static void
281 gst_parse_free_delayed_set (DelayedSet *set)
282 {
283   g_free(set->name);
284   g_free(set->value_str);
285   g_slice_free(DelayedSet, set);
286 }
287
288 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
289     const gchar * name, gpointer data);
290
291 static void
292 gst_parse_add_delayed_set (GstElement *element, gchar *name, gchar *value_str)
293 {
294   DelayedSet *data = g_slice_new0 (DelayedSet);
295
296   GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "delaying property set %s to %s",
297     name, value_str);
298
299   data->name = g_strdup(name);
300   data->value_str = g_strdup(value_str);
301   data->signal_id = g_signal_connect_data(element, "child-added",
302       G_CALLBACK (gst_parse_new_child), data, (GClosureNotify)
303       gst_parse_free_delayed_set, (GConnectFlags) 0);
304
305   /* FIXME: we would need to listen on all intermediate bins too */
306   if (GST_IS_BIN (element)) {
307     gchar **names, **current;
308     GstElement *parent, *child;
309
310     current = names = g_strsplit (name, "::", -1);
311     parent = gst_bin_get_by_name (GST_BIN_CAST (element), current[0]);
312     current++;
313     while (parent && current[0]) {
314       child = gst_bin_get_by_name (GST_BIN (parent), current[0]);
315       if (!child && current[1]) {
316         char *sub_name = g_strjoinv ("::", &current[0]);
317
318         gst_parse_add_delayed_set(parent, sub_name, value_str);
319         g_free (sub_name);
320       }
321       parent = child;
322       current++;
323     }
324     g_strfreev (names);
325   }
326 }
327
328 static void
329 gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
330     const gchar * name, gpointer data)
331 {
332   DelayedSet *set = (DelayedSet *) data;
333   GParamSpec *pspec;
334   GValue v = { 0, };
335   GObject *target = NULL;
336   GType value_type;
337
338   GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "new child %s, checking property %s",
339       name, set->name);
340
341   if (gst_child_proxy_lookup (child_proxy, set->name, &target, &pspec)) {
342     gboolean got_value = FALSE;
343
344     value_type = pspec->value_type;
345
346     GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "parsing delayed property %s as a %s from %s",
347       pspec->name, g_type_name (value_type), set->value_str);
348     g_value_init (&v, value_type);
349     if (gst_value_deserialize (&v, set->value_str))
350       got_value = TRUE;
351     else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
352        GstElement *bin;
353
354        bin = gst_parse_bin_from_description_full (set->value_str, TRUE, NULL,
355            GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS, NULL);
356        if (bin) {
357          g_value_set_object (&v, bin);
358          got_value = TRUE;
359        }
360     }
361     g_signal_handler_disconnect (child_proxy, set->signal_id);
362     if (!got_value)
363       goto error;
364     g_object_set_property (target, pspec->name, &v);
365   } else {
366     const gchar *obj_name = GST_OBJECT_NAME(object);
367     gint len = strlen (obj_name);
368
369     /* do a delayed set */
370     if ((strlen (set->name) > (len + 2)) && !strncmp (set->name, obj_name, len) && !strncmp (&set->name[len], "::", 2)) {
371       gst_parse_add_delayed_set (GST_ELEMENT(child_proxy), set->name, set->value_str);
372     }
373   }
374
375 out:
376   if (G_IS_VALUE (&v))
377     g_value_unset (&v);
378   if (target)
379     g_object_unref (target);
380   return;
381
382 error:
383   GST_CAT_ERROR (GST_CAT_PIPELINE, "could not set property \"%s\" in %"
384       GST_PTR_FORMAT, pspec->name, target);
385   goto out;
386 }
387
388 static void
389 gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
390 {
391   GParamSpec *pspec = NULL;
392   gchar *pos = value;
393   GValue v = { 0, };
394   GObject *target = NULL;
395   GType value_type;
396
397   /* do nothing if assignment is for missing element */
398   if (element == NULL)
399     goto out;
400
401   /* parse the string, so the property name is null-terminated and pos points
402      to the beginning of the value */
403   while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++;
404   if (*pos == '=') {
405     *pos = '\0';
406   } else {
407     *pos = '\0';
408     pos++;
409     while (g_ascii_isspace (*pos)) pos++;
410   }
411   pos++;
412   while (g_ascii_isspace (*pos)) pos++;
413   if (*pos == '"') {
414     pos++;
415     pos[strlen (pos) - 1] = '\0';
416   }
417   gst_parse_unescape (pos);
418
419   if (GST_IS_CHILD_PROXY (element)) {
420     if (!gst_child_proxy_lookup (GST_CHILD_PROXY (element), value, &target, &pspec)) {
421       /* do a delayed set */
422       gst_parse_add_delayed_set (element, value, pos);
423     }
424   } else {
425     pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value);
426     if (pspec != NULL) {
427       target = g_object_ref (element);
428       GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, target, "found %s property", value);
429     } else {
430       SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
431           _("no property \"%s\" in element \"%s\""), value, \
432           GST_ELEMENT_NAME (element));
433     }
434   }
435
436   if (pspec != NULL && target != NULL) {
437     gboolean got_value = FALSE;
438
439     value_type = pspec->value_type;
440
441     GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "parsing property %s as a %s",
442         pspec->name, g_type_name (value_type));
443
444     g_value_init (&v, value_type);
445     if (gst_value_deserialize (&v, pos))
446       got_value = TRUE;
447     else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
448        GstElement *bin;
449
450        bin = gst_parse_bin_from_description_full (pos, TRUE, NULL,
451            GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS, NULL);
452        if (bin) {
453          g_value_set_object (&v, bin);
454          got_value = TRUE;
455        }
456     }
457     if (!got_value)
458       goto error;
459     g_object_set_property (target, pspec->name, &v);
460   }
461
462 out:
463   gst_parse_strfree (value);
464   if (G_IS_VALUE (&v))
465     g_value_unset (&v);
466   if (target)
467     g_object_unref (target);
468   return;
469
470 error:
471   SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
472          _("could not set property \"%s\" in element \"%s\" to \"%s\""),
473          value, GST_ELEMENT_NAME (element), pos);
474   goto out;
475 }
476
477 static inline void
478 gst_parse_free_link (link_t *link)
479 {
480   gst_parse_strfree (link->src_name);
481   gst_parse_strfree (link->sink_name);
482   g_slist_foreach (link->src_pads, (GFunc) gst_parse_strfree, NULL);
483   g_slist_foreach (link->sink_pads, (GFunc) gst_parse_strfree, NULL);
484   g_slist_free (link->src_pads);
485   g_slist_free (link->sink_pads);
486   if (link->caps) gst_caps_unref (link->caps);
487   gst_parse_link_free (link);
488 }
489
490 static void
491 gst_parse_free_delayed_link (DelayedLink *link)
492 {
493   g_free (link->src_pad);
494   g_free (link->sink_pad);
495   if (link->caps) gst_caps_unref (link->caps);
496   g_slice_free (DelayedLink, link);
497 }
498
499 static void
500 gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
501 {
502   DelayedLink *link = data;
503
504   GST_CAT_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s",
505                 GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
506                 GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
507
508   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
509       link->sink_pad, link->caps)) {
510     /* do this here, we don't want to get any problems later on when
511      * unlocking states */
512     GST_CAT_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked",
513                    GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
514                    GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
515     g_signal_handler_disconnect (src, link->signal_id);
516   }
517 }
518
519 /* both padnames and the caps may be NULL */
520 static gboolean
521 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
522                                 GstElement *sink, const gchar *sink_pad,
523                                 GstCaps *caps)
524 {
525   GList *templs = gst_element_class_get_pad_template_list (
526       GST_ELEMENT_GET_CLASS (src));
527
528   for (; templs; templs = templs->next) {
529     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
530     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
531         (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
532     {
533       DelayedLink *data = g_slice_new (DelayedLink);
534
535       /* TODO: maybe we should check if src_pad matches this template's names */
536
537       GST_CAT_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s",
538                      GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (src_pad),
539                      GST_STR_NULL (GST_ELEMENT_NAME (sink)), GST_STR_NULL (sink_pad));
540
541       data->src_pad = g_strdup (src_pad);
542       data->sink = sink;
543       data->sink_pad = g_strdup (sink_pad);
544       if (caps) {
545         data->caps = gst_caps_copy (caps);
546       } else {
547         data->caps = NULL;
548       }
549       data->signal_id = g_signal_connect_data (src, "pad-added",
550           G_CALLBACK (gst_parse_found_pad), data,
551           (GClosureNotify) gst_parse_free_delayed_link, (GConnectFlags) 0);
552       return TRUE;
553     }
554   }
555   return FALSE;
556 }
557
558 /*
559  * performs a link and frees the struct. src and sink elements must be given
560  * return values   0 - link performed
561  *                 1 - link delayed
562  *                <0 - error
563  */
564 static gint
565 gst_parse_perform_link (link_t *link, graph_t *graph)
566 {
567   GstElement *src = link->src;
568   GstElement *sink = link->sink;
569   GSList *srcs = link->src_pads;
570   GSList *sinks = link->sink_pads;
571   g_assert (GST_IS_ELEMENT (src));
572   g_assert (GST_IS_ELEMENT (sink));
573
574   GST_CAT_INFO (GST_CAT_PIPELINE,
575       "linking %s:%s to %s:%s (%u/%u) with caps \"%" GST_PTR_FORMAT "\"",
576       GST_ELEMENT_NAME (src), link->src_name ? link->src_name : "(any)",
577       GST_ELEMENT_NAME (sink), link->sink_name ? link->sink_name : "(any)",
578       g_slist_length (srcs), g_slist_length (sinks), link->caps);
579
580   if (!srcs || !sinks) {
581     if (gst_element_link_pads_filtered (src,
582         srcs ? (const gchar *) srcs->data : NULL, sink,
583         sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
584       goto success;
585     } else {
586       if (gst_parse_perform_delayed_link (src,
587           srcs ? (const gchar *) srcs->data : NULL,
588           sink, sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
589         goto success;
590       } else {
591         goto error;
592       }
593     }
594   }
595   if (g_slist_length (link->src_pads) != g_slist_length (link->sink_pads)) {
596     goto error;
597   }
598   while (srcs && sinks) {
599     const gchar *src_pad = (const gchar *) srcs->data;
600     const gchar *sink_pad = (const gchar *) sinks->data;
601     srcs = g_slist_next (srcs);
602     sinks = g_slist_next (sinks);
603     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
604         link->caps)) {
605       continue;
606     } else {
607       if (gst_parse_perform_delayed_link (src, src_pad,
608                                           sink, sink_pad,
609                                           link->caps)) {
610         continue;
611       } else {
612         goto error;
613       }
614     }
615   }
616
617 success:
618   gst_parse_free_link (link);
619   return 0;
620
621 error:
622   SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
623       _("could not link %s to %s"), GST_ELEMENT_NAME (src),
624       GST_ELEMENT_NAME (sink));
625   gst_parse_free_link (link);
626   return -1;
627 }
628
629
630 static int yyerror (void *scanner, graph_t *graph, const char *s);
631 %}
632
633 %union {
634     gchar *s;
635     chain_t *c;
636     link_t *l;
637     GstElement *e;
638     GSList *p;
639     graph_t *g;
640 }
641
642 %token <s> PARSE_URL
643 %token <s> IDENTIFIER
644 %left <s> REF PADREF BINREF
645 %token <s> ASSIGNMENT
646 %token <s> LINK
647
648 %type <g> graph
649 %type <c> chain bin
650 %type <l> reference
651 %type <l> linkpart link
652 %type <p> linklist
653 %type <e> element
654 %type <p> padlist pads assignments
655
656 %left '(' ')'
657 %left ','
658 %right '.'
659 %left '!' '='
660
661 %lex-param { void *scanner }
662 %parse-param { void *scanner }
663 %parse-param { graph_t *graph }
664 %pure-parser
665
666 %start graph
667 %%
668
669 element:        IDENTIFIER                    { $$ = gst_element_factory_make ($1, NULL);
670                                                 if ($$ == NULL) {
671                                                   ADD_MISSING_ELEMENT (graph, $1);
672                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), $1);
673                                                   /* if FATAL_ERRORS flag is set, we don't have to worry about backwards
674                                                    * compatibility and can continue parsing and check for other missing
675                                                    * elements */
676                                                   if ((graph->flags & GST_PARSE_FLAG_FATAL_ERRORS) == 0) {
677                                                     gst_parse_strfree ($1);
678                                                     YYERROR;
679                                                   }
680                                                 }
681                                                 gst_parse_strfree ($1);
682                                               }
683         |       element ASSIGNMENT            { gst_parse_element_set ($2, $1, graph);
684                                                 $$ = $1;
685                                               }
686         ;
687 assignments:    /* NOP */                     { $$ = NULL; }
688         |       assignments ASSIGNMENT        { $$ = g_slist_prepend ($1, $2); }
689         ;
690 bin:            '(' assignments chain ')' { GST_BIN_MAKE ($$, "bin", $3, $2, no_free); }
691         |       BINREF assignments chain ')'  { GST_BIN_MAKE ($$, $1, $3, $2, gst_parse_strfree);
692                                                 gst_parse_strfree ($1);
693                                               }
694         |       BINREF assignments ')'        { GST_BIN_MAKE ($$, $1, NULL, $2, gst_parse_strfree);
695                                                 gst_parse_strfree ($1);
696                                               }
697         |       BINREF assignments error ')'  { GST_BIN_MAKE ($$, $1, NULL, $2, gst_parse_strfree);
698                                                 gst_parse_strfree ($1);
699                                               }
700         ;
701
702 pads:           PADREF                        { $$ = g_slist_prepend (NULL, $1); }
703         |       PADREF padlist                { $$ = $2;
704                                                 $$ = g_slist_prepend ($$, $1);
705                                               }
706         ;
707 padlist:        ',' IDENTIFIER                { $$ = g_slist_prepend (NULL, $2); }
708         |       ',' IDENTIFIER padlist        { $$ = g_slist_prepend ($3, $2); }
709         ;
710
711 reference:      REF                           { MAKE_REF ($$, $1, NULL); }
712         |       REF padlist                   { MAKE_REF ($$, $1, $2); }
713         ;
714
715 linkpart:       reference                     { $$ = $1; }
716         |       pads                          { MAKE_REF ($$, NULL, $1); }
717         |       /* NOP */                     { MAKE_REF ($$, NULL, NULL); }
718         ;
719
720 link:           linkpart LINK linkpart        { $$ = $1;
721                                                 if ($2) {
722                                                   $$->caps = gst_caps_from_string ($2);
723                                                   if ($$->caps == NULL)
724                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $2);
725                                                   gst_parse_strfree ($2);
726                                                 }
727                                                 $$->sink_name = $3->src_name;
728                                                 $$->sink_pads = $3->src_pads;
729                                                 gst_parse_link_free ($3);
730                                               }
731         ;
732
733 linklist:       link                          { $$ = g_slist_prepend (NULL, $1); }
734         |       link linklist                 { $$ = g_slist_prepend ($2, $1); }
735         |       linklist error                { $$ = $1; }
736         ;
737
738 chain:          element                       { $$ = gst_parse_chain_new ();
739                                                 $$->first = $$->last = $1;
740                                                 $$->front = $$->back = NULL;
741                                                 $$->elements = g_slist_prepend (NULL, $1);
742                                               }
743         |       bin                           { $$ = $1; }
744         |       chain chain                   { if ($1->back && $2->front) {
745                                                   if (!$1->back->sink_name) {
746                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
747                                                     gst_parse_free_link ($1->back);
748                                                   } else {
749                                                     graph->links = g_slist_prepend (graph->links, $1->back);
750                                                   }
751                                                   if (!$2->front->src_name) {
752                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
753                                                     gst_parse_free_link ($2->front);
754                                                   } else {
755                                                     graph->links = g_slist_prepend (graph->links, $2->front);
756                                                   }
757                                                   $1->back = NULL;
758                                                 } else if ($1->back) {
759                                                   if (!$1->back->sink_name) {
760                                                     $1->back->sink = $2->first;
761                                                   }
762                                                 } else if ($2->front) {
763                                                   if (!$2->front->src_name) {
764                                                     $2->front->src = $1->last;
765                                                   }
766                                                   $1->back = $2->front;
767                                                 }
768
769                                                 if ($1->back) {
770                                                   graph->links = g_slist_prepend (graph->links, $1->back);
771                                                 }
772                                                 $1->last = $2->last;
773                                                 $1->back = $2->back;
774                                                 $1->elements = g_slist_concat ($1->elements, $2->elements);
775                                                 if ($2)
776                                                   gst_parse_chain_free ($2);
777                                                 $$ = $1;
778                                               }
779         |       chain linklist                { GSList *walk;
780                                                 if ($1->back) {
781                                                   $2 = g_slist_prepend ($2, $1->back);
782                                                   $1->back = NULL;
783                                                 } else {
784                                                   if (!((link_t *) $2->data)->src_name) {
785                                                     ((link_t *) $2->data)->src = $1->last;
786                                                   }
787                                                 }
788                                                 for (walk = $2; walk; walk = walk->next) {
789                                                   link_t *link = (link_t *) walk->data;
790                                                   if (!link->sink_name && walk->next) {
791                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
792                                                     gst_parse_free_link (link);
793                                                   } else if (!link->src_name && !link->src) {
794                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
795                                                     gst_parse_free_link (link);
796                                                   } else {
797                                                     if (walk->next) {
798                                                       graph->links = g_slist_prepend (graph->links, link);
799                                                     } else {
800                                                       $1->back = link;
801                                                     }
802                                                   }
803                                                 }
804                                                 g_slist_free ($2);
805                                                 $$ = $1;
806                                               }
807         |       chain error                   { $$ = $1; }
808         |       link chain                    { if ($2->front) {
809                                                   if (!$2->front->src_name) {
810                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
811                                                     gst_parse_free_link ($2->front);
812                                                   } else {
813                                                     graph->links = g_slist_prepend (graph->links, $2->front);
814                                                   }
815                                                 }
816                                                 if (!$1->sink_name) {
817                                                   $1->sink = $2->first;
818                                                 }
819                                                 $2->front = $1;
820                                                 $$ = $2;
821                                               }
822         |       PARSE_URL chain               { $$ = $2;
823                                                 if ($$->front) {
824                                                   GstElement *element =
825                                                           gst_element_make_from_uri (GST_URI_SRC, $1, NULL, NULL);
826                                                   /* FIXME: get and parse error properly */
827                                                   if (!element) {
828                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
829                                                             _("no source element for URI \"%s\""), $1);
830                                                   } else {
831                                                     $$->front->src = element;
832                                                     graph->links = g_slist_prepend (
833                                                             graph->links, $$->front);
834                                                     $$->front = NULL;
835                                                     $$->elements = g_slist_prepend ($$->elements, element);
836                                                   }
837                                                 } else {
838                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
839                                                           _("no element to link URI \"%s\" to"), $1);
840                                                 }
841                                                 g_free ($1);
842                                               }
843         |       link PARSE_URL                { GstElement *element =
844                                                           gst_element_make_from_uri (GST_URI_SINK, $2, NULL, NULL);
845                                                 /* FIXME: get and parse error properly */
846                                                 if (!element) {
847                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
848                                                           _("no sink element for URI \"%s\""), $2);
849                                                   gst_parse_link_free ($1);
850                                                   g_free ($2);
851                                                   YYERROR;
852                                                 } else if ($1->sink_name || $1->sink_pads) {
853                                                   gst_object_unref (element);
854                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
855                                                           _("could not link sink element for URI \"%s\""), $2);
856                                                   gst_parse_link_free ($1);
857                                                   g_free ($2);
858                                                   YYERROR;
859                                                 } else {
860                                                   $$ = gst_parse_chain_new ();
861                                                   $$->first = $$->last = element;
862                                                   $$->front = $1;
863                                                   $$->front->sink = element;
864                                                   $$->elements = g_slist_prepend (NULL, element);
865                                                 }
866                                                 g_free ($2);
867                                               }
868         ;
869 graph:          /* NOP */                     { SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
870                                                 $$ = graph;
871                                               }
872         |       chain                         { $$ = graph;
873                                                 if ($1->front) {
874                                                   if (!$1->front->src_name) {
875                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without source element"));
876                                                     gst_parse_free_link ($1->front);
877                                                   } else {
878                                                     $$->links = g_slist_prepend ($$->links, $1->front);
879                                                   }
880                                                   $1->front = NULL;
881                                                 }
882                                                 if ($1->back) {
883                                                   if (!$1->back->sink_name) {
884                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link without sink element"));
885                                                     gst_parse_free_link ($1->back);
886                                                   } else {
887                                                     $$->links = g_slist_prepend ($$->links, $1->back);
888                                                   }
889                                                   $1->back = NULL;
890                                                 }
891                                                 $$->chain = $1;
892                                               }
893         ;
894
895 %%
896
897
898 static int
899 yyerror (void *scanner, graph_t *graph, const char *s)
900 {
901   /* FIXME: This should go into the GError somehow, but how? */
902   GST_WARNING ("Error during parsing: %s", s);
903   return -1;
904 }
905
906
907 GstElement *
908 priv_gst_parse_launch (const gchar *str, GError **error, GstParseContext *ctx,
909     GstParseFlags flags)
910 {
911   graph_t g;
912   gchar *dstr;
913   GSList *walk;
914   GstBin *bin = NULL;
915   GstElement *ret;
916   yyscan_t scanner;
917
918   g_return_val_if_fail (str != NULL, NULL);
919   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
920
921   g.chain = NULL;
922   g.links = NULL;
923   g.error = error;
924   g.ctx = ctx;
925   g.flags = flags;
926
927 #ifdef __GST_PARSE_TRACE
928   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
929   __strings = __chains = __links = 0;
930 #endif /* __GST_PARSE_TRACE */
931
932   dstr = g_strdup (str);
933   priv_gst_parse_yylex_init (&scanner);
934   priv_gst_parse_yy_scan_string (dstr, scanner);
935
936 #ifndef YYDEBUG
937   yydebug = 1;
938 #endif
939
940   if (yyparse (scanner, &g) != 0) {
941     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
942         "Unrecoverable syntax error while parsing pipeline %s", str);
943
944     priv_gst_parse_yylex_destroy (scanner);
945     g_free (dstr);
946
947     goto error1;
948   }
949   priv_gst_parse_yylex_destroy (scanner);
950   g_free (dstr);
951
952   GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
953       g.chain ? g_slist_length (g.chain->elements) : 0,
954       g_slist_length (g.links));
955
956   if (!g.chain) {
957     ret = NULL;
958   } else if (!g.chain->elements->next) {
959     /* only one toplevel element */
960     ret = (GstElement *) g.chain->elements->data;
961     g_slist_free (g.chain->elements);
962     if (GST_IS_BIN (ret))
963       bin = GST_BIN (ret);
964     gst_parse_chain_free (g.chain);
965   } else {
966     /* put all elements in our bin */
967     bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
968     g_assert (bin);
969
970     for (walk = g.chain->elements; walk; walk = walk->next) {
971       if (walk->data != NULL)
972         gst_bin_add (bin, GST_ELEMENT (walk->data));
973     }
974
975     g_slist_free (g.chain->elements);
976     ret = GST_ELEMENT (bin);
977     gst_parse_chain_free (g.chain);
978   }
979
980   /* remove links */
981   for (walk = g.links; walk; walk = walk->next) {
982     link_t *l = (link_t *) walk->data;
983     if (!l->src) {
984       if (l->src_name) {
985         if (bin) {
986           l->src = gst_bin_get_by_name_recurse_up (bin, l->src_name);
987           if (l->src)
988             gst_object_unref (l->src);
989         } else {
990           l->src = strcmp (GST_ELEMENT_NAME (ret), l->src_name) == 0 ? ret : NULL;
991         }
992       }
993       if (!l->src) {
994         if (l->src_name) {
995           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
996               "No element named \"%s\" - omitting link", l->src_name);
997         } else {
998           /* probably a missing element which we've handled already */
999         }
1000         gst_parse_free_link (l);
1001         continue;
1002       }
1003     }
1004     if (!l->sink) {
1005       if (l->sink_name) {
1006         if (bin) {
1007           l->sink = gst_bin_get_by_name_recurse_up (bin, l->sink_name);
1008           if (l->sink)
1009             gst_object_unref (l->sink);
1010         } else {
1011           l->sink = strcmp (GST_ELEMENT_NAME (ret), l->sink_name) == 0 ? ret : NULL;
1012         }
1013       }
1014       if (!l->sink) {
1015         if (l->sink_name) {
1016           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1017               "No element named \"%s\" - omitting link", l->sink_name);
1018         } else {
1019           /* probably a missing element which we've handled already */
1020         }
1021         gst_parse_free_link (l);
1022         continue;
1023       }
1024     }
1025     gst_parse_perform_link (l, &g);
1026   }
1027   g_slist_free (g.links);
1028
1029 out:
1030 #ifdef __GST_PARSE_TRACE
1031   GST_CAT_DEBUG (GST_CAT_PIPELINE,
1032       "TRACE: %u strings, %u chains and %u links left", __strings, __chains,
1033       __links);
1034   if (__strings || __chains || __links) {
1035     g_warning ("TRACE: %u strings, %u chains and %u links left", __strings,
1036         __chains, __links);
1037   }
1038 #endif /* __GST_PARSE_TRACE */
1039
1040   return ret;
1041
1042 error1:
1043   if (g.chain) {
1044     g_slist_foreach (g.chain->elements, (GFunc)gst_object_unref, NULL);
1045     g_slist_free (g.chain->elements);
1046     gst_parse_chain_free (g.chain);
1047   }
1048
1049   g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
1050   g_slist_free (g.links);
1051
1052   if (error)
1053     g_assert (*error);
1054   ret = NULL;
1055
1056   goto out;
1057 }