grammar.y: remove trailing whitespace
[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 /*******************************************************************************************
37 *** Tracing memory leaks
38 *******************************************************************************************/
39
40 #ifdef __GST_PARSE_TRACE
41 static guint __strings;
42 static guint __links;
43 static guint __chains;
44 gchar *
45 __gst_parse_strdup (gchar *org)
46 {
47   gchar *ret;
48   __strings++;
49   ret = g_strdup (org);
50   /* g_print ("ALLOCATED STR   (%3u): %p %s\n", __strings, ret, ret); */
51   return ret;
52 }
53 void
54 __gst_parse_strfree (gchar *str)
55 {
56   if (str) {
57     /* g_print ("FREEING STR     (%3u): %p %s\n", __strings - 1, str, str); */
58     g_free (str);
59     g_return_if_fail (__strings > 0);
60     __strings--;
61   }
62 }
63 link_t *__gst_parse_link_new (void)
64 {
65   link_t *ret;
66   __links++;
67   ret = g_slice_new0 (link_t);
68   /* g_print ("ALLOCATED LINK  (%3u): %p\n", __links, ret); */
69   return ret;
70 }
71 void
72 __gst_parse_link_free (link_t *data)
73 {
74   if (data) {
75     /* g_print ("FREEING LINK    (%3u): %p\n", __links - 1, data); */
76     g_slice_free (link_t, data);
77     g_return_if_fail (__links > 0);
78     __links--;
79   }
80 }
81 chain_t *
82 __gst_parse_chain_new (void)
83 {
84   chain_t *ret;
85   __chains++;
86   ret = g_slice_new0 (chain_t);
87   /* g_print ("@%p: ALLOCATED CHAIN (%3u):\n", ret, __chains); */
88   return ret;
89 }
90 void
91 __gst_parse_chain_free (chain_t *data)
92 {
93   /* g_print ("@%p: FREEING CHAIN   (%3u):\n", data, __chains - 1); */
94   g_slice_free (chain_t, data);
95   g_return_if_fail (__chains > 0);
96   __chains--;
97 }
98
99 #endif /* __GST_PARSE_TRACE */
100
101 /*******************************************************************************************
102 *** define SET_ERROR macro/function
103 *******************************************************************************************/
104 #ifdef G_HAVE_ISO_VARARGS
105
106 #  define SET_ERROR(error, type, ...) \
107 G_STMT_START { \
108   GST_CAT_ERROR (GST_CAT_PIPELINE, __VA_ARGS__); \
109   if ((error) && !*(error)) { \
110     g_set_error ((error), GST_PARSE_ERROR, (type), __VA_ARGS__); \
111   } \
112 } G_STMT_END
113
114 #elif defined(G_HAVE_GNUC_VARARGS)
115
116 #  define SET_ERROR(error, type, args...) \
117 G_STMT_START { \
118   GST_CAT_ERROR (GST_CAT_PIPELINE, args ); \
119   if ((error) && !*(error)) { \
120     g_set_error ((error), GST_PARSE_ERROR, (type), args ); \
121   } \
122 } G_STMT_END
123
124 #else
125
126 static inline void
127 SET_ERROR (GError **error, gint type, const char *format, ...)
128 {
129   if (error) {
130     if (*error) {
131       g_warning ("error while parsing");
132     } else {
133       va_list varargs;
134       char *string;
135
136       va_start (varargs, format);
137       string = g_strdup_vprintf (format, varargs);
138       va_end (varargs);
139
140       g_set_error (error, GST_PARSE_ERROR, type, string);
141
142       g_free (string);
143     }
144   }
145 }
146
147 #endif /* G_HAVE_ISO_VARARGS */
148
149 /*** define YYPRINTF macro/function if we're debugging */
150
151 /* bison 1.35 calls this macro with side effects, we need to make sure the
152    side effects work - crappy bison */
153
154 #ifndef GST_DISABLE_GST_DEBUG
155 #  define YYDEBUG 1
156
157 #  ifdef G_HAVE_ISO_VARARGS
158
159 /* #  define YYFPRINTF(a, ...) GST_CAT_DEBUG (GST_CAT_PIPELINE, __VA_ARGS__) */
160 #    define YYFPRINTF(a, ...) \
161 G_STMT_START { \
162      GST_CAT_LOG (GST_CAT_PIPELINE, __VA_ARGS__); \
163 } G_STMT_END
164
165 #  elif defined(G_HAVE_GNUC_VARARGS)
166
167 #    define YYFPRINTF(a, args...) \
168 G_STMT_START { \
169      GST_CAT_LOG (GST_CAT_PIPELINE, args); \
170 } G_STMT_END
171
172 #  else
173
174 static inline void
175 YYPRINTF(const char *format, ...)
176 {
177   va_list varargs;
178   gchar *temp;
179
180   va_start (varargs, format);
181   temp = g_strdup_vprintf (format, varargs);
182   GST_CAT_LOG (GST_CAT_PIPELINE, "%s", temp);
183   g_free (temp);
184   va_end (varargs);
185 }
186
187 #  endif /* G_HAVE_ISO_VARARGS */
188
189 #endif /* GST_DISABLE_GST_DEBUG */
190
191
192 /*
193  * include headers generated by bison & flex, after defining (or not defining) YYDEBUG
194  */
195 #include "grammar.tab.h"
196 #include "parse_lex.h"
197
198 /*******************************************************************************************
199 *** report missing elements/bins/..
200 *******************************************************************************************/
201
202
203 static void  add_missing_element(graph_t *graph,gchar *name){
204   if ((graph)->ctx){
205     (graph)->ctx->missing_elements = g_list_append ((graph)->ctx->missing_elements, g_strdup (name));
206     }
207 }
208
209
210 /*******************************************************************************************
211 *** helpers for pipeline-setup
212 *******************************************************************************************/
213
214 #define TRY_SETUP_LINK(l) G_STMT_START { \
215    if( (!(l)->src.element) && (!(l)->src.name) ){ \
216      SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link has no source [sink=%s@%p]"), \
217         (l)->sink.name ? (l)->sink.name : _(""), \
218         (l)->sink.element); \
219      gst_parse_free_link (l); \
220    }else if( (!(l)->sink.element) && (!(l)->sink.name) ){ \
221      SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("link has no sink [source=%s@%p]"), \
222         (l)->src.name ? (l)->src.name :_(""), \
223         (l)->src.element); \
224      gst_parse_free_link (l); \
225    }else{ \
226      graph->links = g_slist_append (graph->links, l ); \
227    }   \
228 } G_STMT_END
229
230 typedef struct {
231   gchar *src_pad;
232   gchar *sink_pad;
233   GstElement *sink;
234   GstCaps *caps;
235   gulong signal_id;
236 } DelayedLink;
237
238 typedef struct {
239   gchar *name;
240   gchar *value_str;
241   gulong signal_id;
242 } DelayedSet;
243
244 static int  gst_resolve_reference(reference_t *rr, GstElement *pipeline){
245   GstBin *bin;
246
247   if(rr->element) return  0;  /* already resolved! */
248   if(!rr->name)   return -2;  /* no chance! */
249
250   if (GST_IS_BIN (pipeline)){
251     bin = GST_BIN (pipeline);
252     rr->element = gst_bin_get_by_name_recurse_up (bin, rr->name);
253   } else {
254     rr->element = strcmp (GST_ELEMENT_NAME (pipeline), rr->name) == 0 ?
255                 gst_object_ref(pipeline) : NULL;
256   }
257   if(rr->element) return 0; /* resolved */
258   else            return -1; /* not found */
259 }
260
261 static void gst_parse_free_delayed_set (DelayedSet *set)
262 {
263   g_free(set->name);
264   g_free(set->value_str);
265   g_slice_free(DelayedSet, set);
266 }
267
268 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
269     const gchar * name, gpointer data);
270
271 static void gst_parse_add_delayed_set (GstElement *element, gchar *name, gchar *value_str)
272 {
273   DelayedSet *data = g_slice_new0 (DelayedSet);
274
275   GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "delaying property set %s to %s",
276     name, value_str);
277
278   data->name = g_strdup(name);
279   data->value_str = g_strdup(value_str);
280   data->signal_id = g_signal_connect_data(element, "child-added",
281       G_CALLBACK (gst_parse_new_child), data, (GClosureNotify)
282       gst_parse_free_delayed_set, (GConnectFlags) 0);
283
284   /* FIXME: we would need to listen on all intermediate bins too */
285   if (GST_IS_BIN (element)) {
286     gchar **names, **current;
287     GstElement *parent, *child;
288
289     current = names = g_strsplit (name, "::", -1);
290     parent = gst_bin_get_by_name (GST_BIN_CAST (element), current[0]);
291     current++;
292     while (parent && current[0]) {
293       child = gst_bin_get_by_name (GST_BIN (parent), current[0]);
294       if (!child && current[1]) {
295         char *sub_name = g_strjoinv ("::", &current[0]);
296
297         gst_parse_add_delayed_set(parent, sub_name, value_str);
298         g_free (sub_name);
299       }
300       gst_object_unref (parent);
301       parent = child;
302       current++;
303     }
304     if (parent)
305       gst_object_unref (parent);
306     g_strfreev (names);
307   }
308 }
309
310 static void gst_parse_new_child(GstChildProxy *child_proxy, GObject *object,
311     const gchar * name, gpointer data)
312 {
313   DelayedSet *set = (DelayedSet *) data;
314   GParamSpec *pspec;
315   GValue v = { 0, };
316   GObject *target = NULL;
317   GType value_type;
318
319   GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "new child %s, checking property %s",
320       name, set->name);
321
322   if (gst_child_proxy_lookup (child_proxy, set->name, &target, &pspec)) {
323     gboolean got_value = FALSE;
324
325     value_type = pspec->value_type;
326
327     GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, child_proxy, "parsing delayed property %s as a %s from %s",
328       pspec->name, g_type_name (value_type), set->value_str);
329     g_value_init (&v, value_type);
330     if (gst_value_deserialize (&v, set->value_str))
331       got_value = TRUE;
332     else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
333        GstElement *bin;
334
335        bin = gst_parse_bin_from_description_full (set->value_str, TRUE, NULL,
336            GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS, NULL);
337        if (bin) {
338          g_value_set_object (&v, bin);
339          got_value = TRUE;
340        }
341     }
342     g_signal_handler_disconnect (child_proxy, set->signal_id);
343     if (!got_value)
344       goto error;
345     g_object_set_property (target, pspec->name, &v);
346   } else {
347     const gchar *obj_name = GST_OBJECT_NAME(object);
348     gint len = strlen (obj_name);
349
350     /* do a delayed set */
351     if ((strlen (set->name) > (len + 2)) && !strncmp (set->name, obj_name, len) && !strncmp (&set->name[len], "::", 2)) {
352       gst_parse_add_delayed_set (GST_ELEMENT(child_proxy), set->name, set->value_str);
353     }
354   }
355
356 out:
357   if (G_IS_VALUE (&v))
358     g_value_unset (&v);
359   if (target)
360     g_object_unref (target);
361   return;
362
363 error:
364   GST_CAT_ERROR (GST_CAT_PIPELINE, "could not set property \"%s\" in %"
365       GST_PTR_FORMAT, pspec->name, target);
366   goto out;
367 }
368
369 static void gst_parse_element_set (gchar *value, GstElement *element, graph_t *graph)
370 {
371   GParamSpec *pspec = NULL;
372   gchar *pos = value;
373   GValue v = { 0, };
374   GObject *target = NULL;
375   GType value_type;
376
377   /* do nothing if assignment is for missing element */
378   if (element == NULL)
379     goto out;
380
381   /* parse the string, so the property name is null-terminated and pos points
382      to the beginning of the value */
383   while (!g_ascii_isspace (*pos) && (*pos != '=')) pos++;
384   if (*pos == '=') {
385     *pos = '\0';
386   } else {
387     *pos = '\0';
388     pos++;
389     while (g_ascii_isspace (*pos)) pos++;
390   }
391   pos++;
392   while (g_ascii_isspace (*pos)) pos++;
393   /* truncate a string if it is delimited with double quotes */
394   if (*pos == '"' && pos[strlen (pos) - 1] == '"') {
395     pos++;
396     pos[strlen (pos) - 1] = '\0';
397   }
398   gst_parse_unescape (pos);
399
400   if (GST_IS_CHILD_PROXY (element)) {
401     if (!gst_child_proxy_lookup (GST_CHILD_PROXY (element), value, &target, &pspec)) {
402       /* do a delayed set */
403       gst_parse_add_delayed_set (element, value, pos);
404     }
405   } else {
406     pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (element), value);
407     if (pspec != NULL) {
408       target = g_object_ref (element);
409       GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, target, "found %s property", value);
410     } else {
411       SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_PROPERTY, \
412           _("no property \"%s\" in element \"%s\""), value, \
413           GST_ELEMENT_NAME (element));
414     }
415   }
416
417   if (pspec != NULL && target != NULL) {
418     gboolean got_value = FALSE;
419
420     value_type = pspec->value_type;
421
422     GST_CAT_LOG_OBJECT (GST_CAT_PIPELINE, element, "parsing property %s as a %s",
423         pspec->name, g_type_name (value_type));
424
425     g_value_init (&v, value_type);
426     if (gst_value_deserialize (&v, pos))
427       got_value = TRUE;
428     else if (g_type_is_a (value_type, GST_TYPE_ELEMENT)) {
429        GstElement *bin;
430
431        bin = gst_parse_bin_from_description_full (pos, TRUE, NULL,
432            GST_PARSE_FLAG_NO_SINGLE_ELEMENT_BINS, NULL);
433        if (bin) {
434          g_value_set_object (&v, bin);
435          got_value = TRUE;
436        }
437     }
438     if (!got_value)
439       goto error;
440     g_object_set_property (target, pspec->name, &v);
441   }
442
443 out:
444   gst_parse_strfree (value);
445   if (G_IS_VALUE (&v))
446     g_value_unset (&v);
447   if (target)
448     g_object_unref (target);
449   return;
450
451 error:
452   SET_ERROR (graph->error, GST_PARSE_ERROR_COULD_NOT_SET_PROPERTY,
453          _("could not set property \"%s\" in element \"%s\" to \"%s\""),
454          value, GST_ELEMENT_NAME (element), pos);
455   goto out;
456 }
457
458 static void gst_parse_free_reference (reference_t *rr)
459 {
460   if(rr->element) gst_object_unref(rr->element);
461   gst_parse_strfree (rr->name);
462   g_slist_foreach (rr->pads, (GFunc) gst_parse_strfree, NULL);
463   g_slist_free (rr->pads);
464 }
465
466 static void gst_parse_free_link (link_t *link)
467 {
468   gst_parse_free_reference (&(link->src));
469   gst_parse_free_reference (&(link->sink));
470   if (link->caps) gst_caps_unref (link->caps);
471   gst_parse_link_free (link);
472 }
473
474 static void gst_parse_free_chain (chain_t *ch)
475 {
476   GSList *walk;
477   gst_parse_free_reference (&(ch->first));
478   gst_parse_free_reference (&(ch->last));
479   for(walk=ch->elements;walk;walk=walk->next)
480     gst_object_unref (walk->data);
481   g_slist_free (ch->elements);
482   gst_parse_chain_free (ch);
483 }
484
485 static void gst_parse_free_delayed_link (DelayedLink *link)
486 {
487   g_free (link->src_pad);
488   g_free (link->sink_pad);
489   if (link->caps) gst_caps_unref (link->caps);
490   g_slice_free (DelayedLink, link);
491 }
492
493 static void gst_parse_found_pad (GstElement *src, GstPad *pad, gpointer data)
494 {
495   DelayedLink *link = data;
496
497   GST_CAT_INFO (GST_CAT_PIPELINE, "trying delayed linking %s:%s to %s:%s",
498                 GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
499                 GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
500
501   if (gst_element_link_pads_filtered (src, link->src_pad, link->sink,
502       link->sink_pad, link->caps)) {
503     /* do this here, we don't want to get any problems later on when
504      * unlocking states */
505     GST_CAT_DEBUG (GST_CAT_PIPELINE, "delayed linking %s:%s to %s:%s worked",
506                    GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (link->src_pad),
507                    GST_STR_NULL (GST_ELEMENT_NAME (link->sink)), GST_STR_NULL (link->sink_pad));
508     g_signal_handler_disconnect (src, link->signal_id);
509   }
510 }
511
512 /* both padnames and the caps may be NULL */
513 static gboolean
514 gst_parse_perform_delayed_link (GstElement *src, const gchar *src_pad,
515                                 GstElement *sink, const gchar *sink_pad,
516                                 GstCaps *caps)
517 {
518   GList *templs = gst_element_class_get_pad_template_list (
519       GST_ELEMENT_GET_CLASS (src));
520
521   for (; templs; templs = templs->next) {
522     GstPadTemplate *templ = (GstPadTemplate *) templs->data;
523     if ((GST_PAD_TEMPLATE_DIRECTION (templ) == GST_PAD_SRC) &&
524         (GST_PAD_TEMPLATE_PRESENCE(templ) == GST_PAD_SOMETIMES))
525     {
526       DelayedLink *data = g_slice_new (DelayedLink);
527
528       /* TODO: maybe we should check if src_pad matches this template's names */
529
530       GST_CAT_DEBUG (GST_CAT_PIPELINE, "trying delayed link %s:%s to %s:%s",
531                      GST_STR_NULL (GST_ELEMENT_NAME (src)), GST_STR_NULL (src_pad),
532                      GST_STR_NULL (GST_ELEMENT_NAME (sink)), GST_STR_NULL (sink_pad));
533
534       data->src_pad = g_strdup (src_pad);
535       data->sink = sink;
536       data->sink_pad = g_strdup (sink_pad);
537       if (caps) {
538         data->caps = gst_caps_copy (caps);
539       } else {
540         data->caps = NULL;
541       }
542       data->signal_id = g_signal_connect_data (src, "pad-added",
543           G_CALLBACK (gst_parse_found_pad), data,
544           (GClosureNotify) gst_parse_free_delayed_link, (GConnectFlags) 0);
545       return TRUE;
546     }
547   }
548   return FALSE;
549 }
550
551 /*
552  * performs a link and frees the struct. src and sink elements must be given
553  * return values   0 - link performed
554  *                 1 - link delayed
555  *                <0 - error
556  */
557 static gint
558 gst_parse_perform_link (link_t *link, graph_t *graph)
559 {
560   GstElement *src = link->src.element;
561   GstElement *sink = link->sink.element;
562   GSList *srcs = link->src.pads;
563   GSList *sinks = link->sink.pads;
564   g_assert (GST_IS_ELEMENT (src));
565   g_assert (GST_IS_ELEMENT (sink));
566
567   GST_CAT_INFO (GST_CAT_PIPELINE,
568       "linking %s:%s to %s:%s (%u/%u) with caps \"%" GST_PTR_FORMAT "\"",
569       GST_ELEMENT_NAME (src), link->src.name ? link->src.name : "(any)",
570       GST_ELEMENT_NAME (sink), link->sink.name ? link->sink.name : "(any)",
571       g_slist_length (srcs), g_slist_length (sinks), link->caps);
572
573   if (!srcs || !sinks) {
574     if (gst_element_link_pads_filtered (src,
575         srcs ? (const gchar *) srcs->data : NULL, sink,
576         sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
577       goto success;
578     } else {
579       if (gst_parse_perform_delayed_link (src,
580           srcs ? (const gchar *) srcs->data : NULL,
581           sink, sinks ? (const gchar *) sinks->data : NULL, link->caps)) {
582         goto success;
583       } else {
584         goto error;
585       }
586     }
587   }
588   if (g_slist_length (link->src.pads) != g_slist_length (link->sink.pads)) {
589     goto error;
590   }
591   while (srcs && sinks) {
592     const gchar *src_pad = (const gchar *) srcs->data;
593     const gchar *sink_pad = (const gchar *) sinks->data;
594     srcs = g_slist_next (srcs);
595     sinks = g_slist_next (sinks);
596     if (gst_element_link_pads_filtered (src, src_pad, sink, sink_pad,
597         link->caps)) {
598       continue;
599     } else {
600       if (gst_parse_perform_delayed_link (src, src_pad,
601                                           sink, sink_pad,
602                                           link->caps)) {
603         continue;
604       } else {
605         goto error;
606       }
607     }
608   }
609
610 success:
611   gst_parse_free_link (link);
612   return 0;
613
614 error:
615   SET_ERROR (graph->error, GST_PARSE_ERROR_LINK,
616       _("could not link %s to %s"), GST_ELEMENT_NAME (src),
617       GST_ELEMENT_NAME (sink));
618   gst_parse_free_link (link);
619   return -1;
620 }
621
622
623 static int yyerror (void *scanner, graph_t *graph, const char *s);
624 %}
625
626 %union {
627     gchar *ss;
628     chain_t *cc;
629     link_t *ll;
630     reference_t rr;
631     GstElement *ee;
632     GSList *pp;
633     graph_t *gg;
634 }
635
636 /* No grammar ambiguities expected, FAIL otherwise */
637 %expect 0
638
639 %token <ss> PARSE_URL
640 %token <ss> IDENTIFIER
641 %left  <ss> REF PADREF BINREF
642 %token <ss> ASSIGNMENT
643 %token <ss> LINK
644
645 %type <ss> binopener
646 %type <gg> graph
647 %type <cc> chain bin chainlist openchain elementary
648 %type <rr> reference
649 %type <ll> link
650 %type <ee> element
651 %type <pp> morepads pads assignments
652
653 %destructor {   gst_parse_strfree ($$);         } <ss>
654 %destructor {   if($$)
655                   gst_parse_free_chain($$);     } <cc>
656 %destructor {   gst_parse_free_link ($$);       } <ll>
657 %destructor {   gst_parse_free_reference(&($$));} <rr>
658 %destructor {   gst_object_unref ($$);          } <ee>
659 %destructor {   GSList *walk;
660                 for(walk=$$;walk;walk=walk->next)
661                   gst_parse_strfree (walk->data);
662                 g_slist_free ($$);              } <pp>
663
664
665
666 %left '(' ')'
667 %left ','
668 %right '.'
669 %left '!' '='
670
671 %lex-param { void *scanner }
672 %parse-param { void *scanner }
673 %parse-param { graph_t *graph }
674 %pure-parser
675
676 %start graph
677 %%
678
679 /*************************************************************
680 * Grammar explanation:
681 *   _element_s are specified by an identifier of their type.
682 *   a name can be give in the optional property-assignments
683 *       coffeeelement
684 *       fakesrc name=john
685 *       identity silence=false name=frodo
686 *   (cont'd)
687 **************************************************************/
688 element:        IDENTIFIER                    { $$ = gst_element_factory_make ($1, NULL);
689                                                 if ($$ == NULL) {
690                                                   add_missing_element(graph, $1);
691                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT, _("no element \"%s\""), $1);
692                                                 }
693                                                 gst_parse_strfree ($1);
694                                               }
695         |       element ASSIGNMENT            { gst_parse_element_set ($2, $1, graph);
696                                                 $$ = $1;
697                                               }
698         ;
699
700 /*************************************************************
701 * Grammar explanation: (cont'd)
702 *   a graph has (pure) _element_s, _bin_s and _link_s.
703 *   since bins are special elements, bins and elements can
704 *   be generalized as _elementary_.
705 *   The construction of _bin_s will be discussed later.
706 *   (cont'd)
707 *
708 **************************************************************/
709 elementary:
710         element                               { $$ = gst_parse_chain_new ();
711                                                 /* g_print ("@%p: CHAINing elementary\n", $$); */
712                                                 $$->first.element = $1? gst_object_ref($1) : NULL;
713                                                 $$->last.element = $1? gst_object_ref($1) : NULL;
714                                                 $$->first.name = $$->last.name = NULL;
715                                                 $$->first.pads = $$->last.pads = NULL;
716                                                 $$->elements = $1 ? g_slist_prepend (NULL, $1) : NULL;
717                                               }
718         | bin                                 { $$=$1; }
719         ;
720
721 /*************************************************************
722 * Grammar explanation: (cont'd)
723 *   a _chain_ is a list of _elementary_s that have _link_s inbetween
724 *   which are represented through infix-notation.
725 *
726 *       fakesrc ! sometransformation ! fakesink
727 *
728 *   every _link_ can be augmented with _pads_.
729 *
730 *       coffeesrc .sound ! speakersink
731 *       multisrc  .movie,ads ! .projector,smallscreen multisink
732 *
733 *   and every _link_ can be setup to filter media-types
734 *       mediasrc ! audio/x-raw, signed=TRUE ! stereosink
735 *
736 * User HINT:
737 *   if the lexer does not recognize your media-type it
738 *   will make it an element name. that results in errors
739 *   like
740 *       NO SUCH ELEMENT: no element audio7x-raw
741 *   '7' vs. '/' in https://en.wikipedia.org/wiki/QWERTZ
742 *
743 * Parsing HINT:
744 *   in the parser we need to differ between chains that can
745 *   be extended by more elementaries (_openchain_) and others
746 *   that are syntactically closed (handled later in this file).
747 *       (e.g. fakesrc ! sinkreferencename.padname)
748 **************************************************************/
749 chain:  openchain                             { $$=$1;
750                                                 if($$->last.name){
751                                                         SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
752                                                         _("unexpected reference \"%s\" - ignoring"), $$->last.name);
753                                                         gst_parse_strfree($$->last.name);
754                                                         $$->last.name=NULL;
755                                                 }
756                                                 if($$->last.pads){
757                                                         SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX,
758                                                         _("unexpected pad-reference \"%s\" - ignoring"), (gchar*)$$->last.pads->data);
759                                                         g_slist_foreach ($$->last.pads, (GFunc) gst_parse_strfree, NULL);
760                                                         g_slist_free ($$->last.pads);
761                                                         $$->last.pads=NULL;
762                                                 }
763                                               }
764         ;
765
766 openchain:
767         elementary pads                       { $$=$1;
768                                                 $$->last.pads = g_slist_concat ($$->last.pads, $2);
769                                                 /* g_print ("@%p@%p: FKI elementary pads\n", $1, $$->last.pads); */
770                                               }
771         | openchain link pads elementary pads
772                                               {
773                                                 $2->src  = $1->last;
774                                                 $2->sink = $4->first;
775                                                 $2->sink.pads = g_slist_concat ($3, $2->sink.pads);
776                                                 TRY_SETUP_LINK($2);
777                                                 $4->first = $1->first;
778                                                 $4->elements = g_slist_concat ($1->elements, $4->elements);
779                                                 gst_parse_chain_free($1);
780                                                 $$ = $4;
781                                                 $$->last.pads = g_slist_concat ($$->last.pads, $5);
782                                               }
783
784         ;
785
786 link:   LINK                                  { $$ = gst_parse_link_new ();
787                                                 $$->src.element = NULL;
788                                                 $$->sink.element = NULL;
789                                                 $$->src.name = NULL;
790                                                 $$->sink.name = NULL;
791                                                 $$->src.pads = NULL;
792                                                 $$->sink.pads = NULL;
793                                                 $$->caps = NULL;
794                                                 if ($1) {
795                                                   $$->caps = gst_caps_from_string ($1);
796                                                   if ($$->caps == NULL)
797                                                     SET_ERROR (graph->error, GST_PARSE_ERROR_LINK, _("could not parse caps \"%s\""), $1);
798                                                   gst_parse_strfree ($1);
799                                                 }
800                                               }
801         ;
802 pads:           /* NOP */                     { $$ = NULL; }
803         |       PADREF morepads               { $$ = $2;
804                                                 $$ = g_slist_prepend ($$, $1);
805                                               }
806         ;
807 morepads:       /* NOP */                     { $$ = NULL; }
808         |       ',' IDENTIFIER morepads       { $$ = g_slist_prepend ($3, $2); }
809         ;
810
811 /*************************************************************
812 * Grammar explanation: (cont'd)
813 *   the first and last elements of a _chain_ can be give
814 *   as URL. This creates special elements that fit the URL.
815 *
816 *       fakesrc ! http://fake-sink.org
817 *       http://somesource.org ! fakesink
818 **************************************************************/
819
820 chain:  openchain link PARSE_URL              { GstElement *element =
821                                                           gst_element_make_from_uri (GST_URI_SINK, $3, NULL, NULL);
822                                                 /* FIXME: get and parse error properly */
823                                                 if (!element) {
824                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
825                                                           _("no sink element for URI \"%s\""), $3);
826                                                 }
827                                                 $$ = $1;
828                                                 $2->sink.element = element?gst_object_ref(element):NULL;
829                                                 $2->src = $1->last;
830                                                 TRY_SETUP_LINK($2);
831                                                 $$->last.element = NULL;
832                                                 $$->last.name = NULL;
833                                                 $$->last.pads = NULL;
834                                                 if(element) $$->elements = g_slist_append ($$->elements, element);
835                                                 g_free ($3);
836                                               }
837         ;
838 openchain:
839         PARSE_URL                             { GstElement *element =
840                                                           gst_element_make_from_uri (GST_URI_SRC, $1, NULL, NULL);
841                                                 /* FIXME: get and parse error properly */
842                                                 if (!element) {
843                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
844                                                     _("no source element for URI \"%s\""), $1);
845                                                 }
846                                                 $$ = gst_parse_chain_new ();
847                                                 /* g_print ("@%p: CHAINing srcURL\n", $$); */
848                                                 $$->first.element = NULL;
849                                                 $$->first.name = NULL;
850                                                 $$->first.pads = NULL;
851                                                 $$->last.element = element ? gst_object_ref(element):NULL;
852                                                 $$->last.name = NULL;
853                                                 $$->last.pads = NULL;
854                                                 $$->elements = element ? g_slist_prepend (NULL, element)  : NULL;
855                                                 g_free($1);
856                                               }
857         ;
858
859
860 /*************************************************************
861 * Grammar explanation: (cont'd)
862 *   the first and last elements of a _chain_ can be linked
863 *   to a named _reference_ (with optional pads).
864 *
865 *       fakesrc ! nameOfSinkElement.
866 *       fakesrc ! nameOfSinkElement.Padname
867 *       fakesrc ! nameOfSinkElement.Padname, anotherPad
868 *       nameOfSource.Padname ! fakesink
869 **************************************************************/
870
871 chain:  openchain link reference              { $$ = $1;
872                                                 $2->sink= $3;
873                                                 $2->src = $1->last;
874                                                 TRY_SETUP_LINK($2);
875                                                 $$->last.element = NULL;
876                                                 $$->last.name = NULL;
877                                                 $$->last.pads = NULL;
878                                               }
879         ;
880
881
882 openchain:
883         reference                             { $$ = gst_parse_chain_new ();
884                                                 $$->last=$1;
885                                                 $$->first.element = NULL;
886                                                 $$->first.name = NULL;
887                                                 $$->first.pads = NULL;
888                                                 $$->elements = NULL;
889                                               }
890         ;
891 reference:      REF morepads                  {
892                                                 gchar *padname = $1;
893                                                 GSList *pads = $2;
894                                                 if (padname) {
895                                                   while (*padname != '.') padname++;
896                                                   *padname = '\0';
897                                                   padname++;
898                                                   if (*padname != '\0')
899                                                     pads = g_slist_prepend (pads, gst_parse_strdup (padname));
900                                                 }
901                                                 $$.element=NULL;
902                                                 $$.name=$1;
903                                                 $$.pads=pads;
904                                               }
905         ;
906
907
908 /*************************************************************
909 * Grammar explanation: (cont'd)
910 *   a _chainlist_ is just a list of _chain_s.
911 *
912 *   You can specify _link_s with named
913 *   _reference_ on each side. That
914 *   works already after the explanations above.
915 *       someSourceName.Pad ! someSinkName.
916 *       someSourceName.Pad,anotherPad ! someSinkName.Apad,Bpad
917 *
918 *   If a syntax error occurs, the already finished _chain_s
919 *   and _links_ are kept intact.
920 *************************************************************/
921
922 chainlist: /* NOP */                          { $$ = NULL; }
923         | chainlist chain                     { if ($1){
924                                                   gst_parse_free_reference(&($1->last));
925                                                   gst_parse_free_reference(&($2->first));
926                                                   $2->first = $1->first;
927                                                   $2->elements = g_slist_concat ($1->elements, $2->elements);
928                                                   gst_parse_chain_free ($1);
929                                                 }
930                                                 $$ = $2;
931                                               }
932         | chainlist error                     { $$=$1;
933                                                 GST_CAT_DEBUG (GST_CAT_PIPELINE,"trying to recover from syntax error");
934                                                 SET_ERROR (graph->error, GST_PARSE_ERROR_SYNTAX, _("syntax error"));
935                                               }
936         ;
937
938 /*************************************************************
939 * Grammar explanation: (cont'd)
940 *   _bins_
941 *************************************************************/
942
943
944 assignments:    /* NOP */                     { $$ = NULL; }
945         |       ASSIGNMENT assignments        { $$ = g_slist_prepend ($2, $1); }
946         ;
947
948 binopener:      '('                           { $$ = gst_parse_strdup(_("bin")); }
949         |       BINREF                        { $$ = $1; }
950         ;
951 bin:    binopener assignments chainlist ')'   {
952                                                 chain_t *chain = $3;
953                                                 GSList *walk;
954                                                 GstBin *bin = (GstBin *) gst_element_factory_make ($1, NULL);
955                                                 if (!chain) {
956                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY_BIN,
957                                                     _("specified empty bin \"%s\", not allowed"), $1);
958                                                   chain = gst_parse_chain_new ();
959                                                   chain->first.element = chain->last.element = NULL;
960                                                   chain->first.name    = chain->last.name    = NULL;
961                                                   chain->first.pads    = chain->last.pads    = NULL;
962                                                   chain->elements = NULL;
963                                                 }
964                                                 if (!bin) {
965                                                   add_missing_element(graph, $1);
966                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
967                                                     _("no bin \"%s\", unpacking elements"), $1);
968                                                   /* clear property-list */
969                                                   g_slist_foreach ($2, (GFunc) gst_parse_strfree, NULL);
970                                                   g_slist_free ($2);
971                                                   $2 = NULL;
972                                                 } else {
973                                                   for (walk = chain->elements; walk; walk = walk->next )
974                                                     gst_bin_add (bin, GST_ELEMENT (walk->data));
975                                                   g_slist_free (chain->elements);
976                                                   chain->elements = g_slist_prepend (NULL, bin);
977                                                 }
978                                                 $$ = chain;
979                                                 /* set the properties now
980                                                  * HINT: property-list cleared above, if bin==NULL
981                                                  */
982                                                 for (walk = $2; walk; walk = walk->next)
983                                                   gst_parse_element_set ((gchar *) walk->data,
984                                                         GST_ELEMENT (bin), graph);
985                                                 g_slist_free ($2);
986                                                 gst_parse_strfree ($1);
987                                               }
988         ;
989
990 /*************************************************************
991 * Grammar explanation: (cont'd)
992 *   _graph_
993 *************************************************************/
994
995 graph:  chainlist                             { $$ = graph;
996                                                 $$->chain = $1;
997                                                 if(!$1) {
998                                                   SET_ERROR (graph->error, GST_PARSE_ERROR_EMPTY, _("empty pipeline not allowed"));
999                                                 }
1000                                               }
1001         ;
1002
1003 %%
1004
1005
1006 static int
1007 yyerror (void *scanner, graph_t *graph, const char *s)
1008 {
1009   /* FIXME: This should go into the GError somehow, but how? */
1010   GST_WARNING ("Error during parsing: %s", s);
1011   return -1;
1012 }
1013
1014
1015 GstElement *
1016 priv_gst_parse_launch (const gchar *str, GError **error, GstParseContext *ctx,
1017     GstParseFlags flags)
1018 {
1019   graph_t g;
1020   gchar *dstr;
1021   GSList *walk;
1022   GstBin *bin = NULL;
1023   GstElement *ret;
1024   yyscan_t scanner;
1025
1026   g_return_val_if_fail (str != NULL, NULL);
1027   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1028
1029   g.chain = NULL;
1030   g.links = NULL;
1031   g.error = error;
1032   g.ctx = ctx;
1033   g.flags = flags;
1034
1035 #ifdef __GST_PARSE_TRACE
1036   GST_CAT_DEBUG (GST_CAT_PIPELINE, "TRACE: tracing enabled");
1037   __strings = __chains = __links = 0;
1038 #endif /* __GST_PARSE_TRACE */
1039
1040   /* g_print("Now scanning: %s\n", str); */
1041
1042   dstr = g_strdup (str);
1043   priv_gst_parse_yylex_init (&scanner);
1044   priv_gst_parse_yy_scan_string (dstr, scanner);
1045
1046 #if YYDEBUG
1047   yydebug = 1;
1048 #endif
1049
1050   if (yyparse (scanner, &g) != 0) {
1051     SET_ERROR (error, GST_PARSE_ERROR_SYNTAX,
1052         "Unrecoverable syntax error while parsing pipeline %s", str);
1053
1054     priv_gst_parse_yylex_destroy (scanner);
1055     g_free (dstr);
1056
1057     goto error1;
1058   }
1059   priv_gst_parse_yylex_destroy (scanner);
1060   g_free (dstr);
1061
1062   GST_CAT_DEBUG (GST_CAT_PIPELINE, "got %u elements and %u links",
1063       g.chain ? g_slist_length (g.chain->elements) : 0,
1064       g_slist_length (g.links));
1065
1066   /* ensure chain is not NULL */
1067   if (!g.chain){
1068     g.chain=gst_parse_chain_new ();
1069     g.chain->elements=NULL;
1070     g.chain->first.element=NULL;
1071     g.chain->first.name=NULL;
1072     g.chain->first.pads=NULL;
1073     g.chain->last.element=NULL;
1074     g.chain->last.name=NULL;
1075     g.chain->last.pads=NULL;
1076   };
1077
1078   /* ensure elements is not empty */
1079   if(!g.chain->elements){
1080     g.chain->elements= g_slist_prepend (NULL, NULL);
1081   };
1082
1083   /* put all elements in our bin if necessary */
1084   if(g.chain->elements->next){
1085     bin = GST_BIN (gst_element_factory_make ("pipeline", NULL));
1086     g_assert (bin);
1087
1088     for (walk = g.chain->elements; walk; walk = walk->next) {
1089       if (walk->data != NULL)
1090         gst_bin_add (bin, GST_ELEMENT (walk->data));
1091     }
1092     g_slist_free (g.chain->elements);
1093     g.chain->elements = g_slist_prepend (NULL, bin);
1094   }
1095
1096   ret = (GstElement *) g.chain->elements->data;
1097   g_slist_free (g.chain->elements);
1098   g.chain->elements=NULL;
1099   if (GST_IS_BIN (ret))
1100     bin = GST_BIN (ret);
1101   gst_parse_free_chain (g.chain);
1102   g.chain = NULL;
1103
1104
1105   /* resolve and perform links */
1106   for (walk = g.links; walk; walk = walk->next) {
1107     link_t *l = (link_t *) walk->data;
1108     int err;
1109     err=gst_resolve_reference( &(l->src), ret);
1110     if (err) {
1111        if(-1==err){
1112           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1113               "No src-element named \"%s\" - omitting link", l->src.name);
1114        }else{
1115           /* probably a missing element which we've handled already */
1116           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1117               "No src-element found - omitting link");
1118        }
1119        gst_parse_free_link (l);
1120        continue;
1121     }
1122
1123     err=gst_resolve_reference( &(l->sink), ret);
1124     if (err) {
1125        if(-1==err){
1126           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1127               "No sink-element named \"%s\" - omitting link", l->src.name);
1128        }else{
1129           /* probably a missing element which we've handled already */
1130           SET_ERROR (error, GST_PARSE_ERROR_NO_SUCH_ELEMENT,
1131               "No sink-element found - omitting link");
1132        }
1133        gst_parse_free_link (l);
1134        continue;
1135     }
1136     gst_parse_perform_link (l, &g);
1137   }
1138   g_slist_free (g.links);
1139
1140 out:
1141 #ifdef __GST_PARSE_TRACE
1142   GST_CAT_DEBUG (GST_CAT_PIPELINE,
1143       "TRACE: %u strings, %u chains and %u links left", __strings, __chains,
1144       __links);
1145   if (__strings || __chains || __links) {
1146     g_warning ("TRACE: %u strings, %u chains and %u links left", __strings,
1147         __chains, __links);
1148   }
1149 #endif /* __GST_PARSE_TRACE */
1150
1151   return ret;
1152
1153 error1:
1154   if (g.chain) {
1155     gst_parse_free_chain (g.chain);
1156     g.chain=NULL;
1157   }
1158
1159   g_slist_foreach (g.links, (GFunc)gst_parse_free_link, NULL);
1160   g_slist_free (g.links);
1161
1162   if (error)
1163     g_assert (*error);
1164   ret = NULL;
1165
1166   goto out;
1167 }