[template-glib] expr: add printerr() builtin



commit d4ce68515920074a70928aa1ffa6bbcb0f429e3b
Author: Christian Hergert <chergert redhat com>
Date:   Wed May 4 17:41:44 2022 -0700

    expr: add printerr() builtin
    
    This is just handy to write to stderr intead of stdout.

 src/tmpl-expr-eval.c    | 21 +++++++++++++++++++++
 src/tmpl-expr-scanner.l |  1 +
 src/tmpl-expr-types.h   |  1 +
 3 files changed, 23 insertions(+)
---
diff --git a/src/tmpl-expr-eval.c b/src/tmpl-expr-eval.c
index d2860b1..98081ab 100644
--- a/src/tmpl-expr-eval.c
+++ b/src/tmpl-expr-eval.c
@@ -65,6 +65,9 @@ static gboolean builtin_log                  (const GValue  *value,
 static gboolean builtin_print                (const GValue  *value,
                                               GValue        *return_value,
                                               GError       **error);
+static gboolean builtin_printerr             (const GValue  *value,
+                                              GValue        *return_value,
+                                              GError       **error);
 static gboolean builtin_repr                 (const GValue  *value,
                                               GValue        *return_value,
                                               GError       **error);
@@ -111,6 +114,7 @@ static BuiltinFunc builtin_funcs [] = {
   builtin_sin,
   builtin_tan,
   builtin_cos,
+  builtin_printerr,
 };
 
 static inline guint
@@ -1854,6 +1858,23 @@ builtin_print (const GValue  *value,
   return TRUE;
 }
 
+static gboolean
+builtin_printerr (const GValue  *value,
+                  GValue        *return_value,
+                  GError       **error)
+{
+  gchar *repr;
+
+  repr = tmpl_value_repr (value);
+  g_printerr ("%s\n", repr);
+  g_free (repr);
+
+  g_value_init (return_value, G_TYPE_BOOLEAN);
+  g_value_set_boolean (return_value, TRUE);
+
+  return TRUE;
+}
+
 static gboolean
 builtin_typeof (const GValue  *value,
                 GValue        *return_value,
diff --git a/src/tmpl-expr-scanner.l b/src/tmpl-expr-scanner.l
index fc8f7a9..ac88596 100644
--- a/src/tmpl-expr-scanner.l
+++ b/src/tmpl-expr-scanner.l
@@ -101,6 +101,7 @@ TmplExprParser *parser = yyextra;
 "sin"    { yylval->fn = TMPL_EXPR_BUILTIN_SIN; return BUILTIN; }
 "tan"    { yylval->fn = TMPL_EXPR_BUILTIN_TAN; return BUILTIN; }
 "cos"    { yylval->fn = TMPL_EXPR_BUILTIN_COS; return BUILTIN; }
+"printerr" { yylval->fn = TMPL_EXPR_BUILTIN_PRINTERR; return BUILTIN; }
 
  /* string literals */
 L?\"(\\.|[^\\"])*\" { yylval->s = copy_literal (yytext); return STRING_LITERAL; }
diff --git a/src/tmpl-expr-types.h b/src/tmpl-expr-types.h
index 84ee57b..6862ff2 100644
--- a/src/tmpl-expr-types.h
+++ b/src/tmpl-expr-types.h
@@ -95,6 +95,7 @@ typedef enum
   TMPL_EXPR_BUILTIN_SIN,
   TMPL_EXPR_BUILTIN_TAN,
   TMPL_EXPR_BUILTIN_COS,
+  TMPL_EXPR_BUILTIN_PRINTERR,
 } TmplExprBuiltin;
 
 TMPL_AVAILABLE_IN_ALL


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]