When doing an if-test on the result of a function which returns a string
authorPeter Maydell <pmaydell@chiark.greenend.org.uk>
Sun, 3 Aug 2008 13:32:17 +0000 (13:32 +0000)
committerPeter Maydell <pmaydell@chiark.greenend.org.uk>
Sun, 3 Aug 2008 13:32:17 +0000 (13:32 +0000)
result, check whether the string is non-empty (as the documentation says we
do). Previously we were always testing the integer 'value'.

ChangeLog
sbr/fmt_compile.c
test/tests/repl/test-if-str [new file with mode: 0644]

index 7e6b2eb..d76efca 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-08-03  Peter Maydell  <pmaydell@chiark.greenend.org.uk>
+
+       * sbr/fmt_compile.c: when doing an if-test on the result
+       of a function which returns a string result, check whether
+       the string is non-empty (as the documentation says we do).
+       Previously we were always testing the integer 'value'. Bug
+       spotted by Eric Gillespie.
+       * test/tests/repl/test-if-str: test case for this bug.
+
 2008-07-24  Eric Gillespie  <epg@pretzelnet.org>
 
        * test/setup-test: Run 'make clean' before building
 2008-07-24  Eric Gillespie  <epg@pretzelnet.org>
 
        * test/setup-test: Run 'make clean' before building
index d699916..c42f1e3 100644 (file)
@@ -53,6 +53,9 @@ extern struct mailname fmt_mnull;
 #define        TF_NOP     8        /* like expr but no result            */
 
 /* ftable->flags */
 #define        TF_NOP     8        /* like expr but no result            */
 
 /* ftable->flags */
+/* NB that TFL_PUTS is also used to decide whether the test
+ * in a "%<(function)..." should be a string or numeric one.
+ */
 #define        TFL_PUTS   1        /* implicit putstr if top level */
 #define        TFL_PUTN   2        /* implicit putnum if top level */
 
 #define        TFL_PUTS   1        /* implicit putstr if top level */
 #define        TFL_PUTN   2        /* implicit putnum if top level */
 
@@ -610,7 +613,14 @@ do_if(char *sp)
                if (ftbl->f_type >= IF_FUNCS)
                    fp->f_type = ftbl->extra;
                else {
                if (ftbl->f_type >= IF_FUNCS)
                    fp->f_type = ftbl->extra;
                else {
-                   LV (FT_IF_V_NE, 0);
+                   /* Put out a string test or a value test depending
+                    * on what this function's return type is.
+                    */
+                   if (ftbl->flags & TFL_PUTS) {
+                       LV (FT_IF_S, 0);
+                   } else {
+                       LV (FT_IF_V_NE, 0);
+                   }
                }
            }
            else {
                }
            }
            else {
diff --git a/test/tests/repl/test-if-str b/test/tests/repl/test-if-str
new file mode 100644 (file)
index 0000000..7582a7d
--- /dev/null
@@ -0,0 +1,36 @@
+#!/bin/sh
+######################################################
+#
+# Test that an '%<(function)...' if-construct correctly
+# tests whether 'str' is empty if the function returns
+# a string, and tests 'value' if the function returns
+# an integer.
+#
+######################################################
+
+# create test replgroupcomps
+form=$MH_TEST_DIR/$$.replgroupcomps
+cat > $form <<EOF
+X-STRING: %(num)%(lit)%<(lit FOO) found%| missing%>\n\
+X-NUMBER: %(num)%(lit)%<(num 3) yes%| no%>\n\
+X-NOSTRING: %(num 3)%(lit x)%<(lit) found%| missing%>\n\
+X-NONUMBER: %(num 3)%(lit x)%<(num) yes%| no%>\n\
+--------
+EOF
+
+expected=$MH_TEST_DIR/$$.expected
+actual=$MH_TEST_DIR/Mail/draft
+
+cat > $expected <<EOF
+X-STRING: 0 found
+X-NUMBER: 0 yes
+X-NOSTRING: 3x missing
+X-NONUMBER: 3x no
+--------
+Test1 writes:
+> This is message number 1
+EOF
+
+repl -editor true -format -form $form -group -nocc me -nowhatnowproc 1
+
+diff -u $expected $actual