From 4e342b9c38c26c4e61196a39eb350665025ecdef Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 16 Mar 2008 22:38:17 +0000 Subject: [PATCH] Patch #6452. Reviewed by John Darrington. * variable-display.c (cmd_variable_width): Fix parsing bug. Also, limit variable display widths to within range 1 thru 2 * MAX_STRING. * automake.mk: Add new test. * command/variable-display.sh: New test. --- src/language/dictionary/ChangeLog | 8 ++ src/language/dictionary/variable-display.c | 34 ++++++-- tests/ChangeLog | 8 ++ tests/automake.mk | 1 + tests/command/variable-display.sh | 97 ++++++++++++++++++++++ 5 files changed, 139 insertions(+), 9 deletions(-) create mode 100755 tests/command/variable-display.sh diff --git a/src/language/dictionary/ChangeLog b/src/language/dictionary/ChangeLog index bc7d7134..20778a24 100644 --- a/src/language/dictionary/ChangeLog +++ b/src/language/dictionary/ChangeLog @@ -1,3 +1,11 @@ +2008-03-16 Ben Pfaff + + Patch #6452. Reviewed by John Darrington. + + * variable-display.c (cmd_variable_width): Fix parsing bug. Also, + limit variable display widths to within range 1 thru 2 * + MAX_STRING. + 2007-11-11 Ben Pfaff * sys-file-info.c (display_variables): Fix DISPLAY LABELS. Thanks diff --git a/src/language/dictionary/variable-display.c b/src/language/dictionary/variable-display.c index bada0814..277db48e 100644 --- a/src/language/dictionary/variable-display.c +++ b/src/language/dictionary/variable-display.c @@ -29,6 +29,9 @@ #include "xalloc.h" +#include "gettext.h" +#define _(msgid) gettext (msgid) + /* Set variables' alignment This is the alignment for GUI display only. It affects nothing but GUIs @@ -91,23 +94,36 @@ cmd_variable_width (struct lexer *lexer, struct dataset *ds) do { struct variable **v; + long int width; size_t nv; size_t i; if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE)) return CMD_FAILURE; - if ( lex_force_match (lexer, '(') ) - { - if ( lex_force_int (lexer)) - lex_get (lexer); - else - return CMD_FAILURE; - lex_force_match (lexer, ')'); - } + if (!lex_force_match (lexer, '(') || !lex_force_int (lexer)) + { + free (v); + return CMD_FAILURE; + } + width = lex_integer (lexer); + lex_get (lexer); + if (!lex_force_match (lexer, ')')) + { + free (v); + return CMD_FAILURE; + } + + if (width < 0) + { + msg (SE, _("Variable display width must be a positive integer.")); + free (v); + return CMD_FAILURE; + } + width = MIN (width, 2 * MAX_STRING); for( i = 0 ; i < nv ; ++i ) - var_set_display_width (v[i], lex_integer (lexer)); + var_set_display_width (v[i], width); while (lex_token (lexer) == '/') lex_get (lexer); diff --git a/tests/ChangeLog b/tests/ChangeLog index ab6430b1..7a601c4e 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,3 +1,11 @@ +2008-03-16 Ben Pfaff + + Patch #6452. Reviewed by John Darrington. + + * automake.mk: Add new test. + + * command/variable-display.sh: New test. + 2008-03-04 Ben Pfaff Patch #6441. Reviewed by John Darrington. diff --git a/tests/automake.mk b/tests/automake.mk index 75989222..abc327a5 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -68,6 +68,7 @@ dist_TESTS = \ tests/command/trimmed-mean.sh \ tests/command/tabs.sh \ tests/command/use.sh \ + tests/command/variable-display.sh \ tests/command/vector.sh \ tests/command/very-long-strings.sh \ tests/command/weight.sh \ diff --git a/tests/command/variable-display.sh b/tests/command/variable-display.sh new file mode 100755 index 00000000..bb639d10 --- /dev/null +++ b/tests/command/variable-display.sh @@ -0,0 +1,97 @@ +#!/bin/sh + +# This program tests variable display attribute commands: VARIABLE +# ALIGNMENT, VARIABLE WIDTH, VARIABLE LEVEL. + +TEMPDIR=/tmp/pspp-tst-$$ +TESTFILE=$TEMPDIR/`basename $0`.sps + +# ensure that top_builddir are absolute +if [ -z "$top_builddir" ] ; then top_builddir=. ; fi +if [ -z "$top_srcdir" ] ; then top_srcdir=. ; fi +top_builddir=`cd $top_builddir; pwd` +PSPP=$top_builddir/src/ui/terminal/pspp + +# ensure that top_srcdir is absolute +top_srcdir=`cd $top_srcdir; pwd` + +STAT_CONFIG_PATH=$top_srcdir/config +export STAT_CONFIG_PATH + + +cleanup() +{ + cd / + rm -rf $TEMPDIR +} + + +fail() +{ + echo $activity + echo FAILED + cleanup; + exit 1; +} + + +no_result() +{ + echo $activity + echo NO RESULT; + cleanup; + exit 2; +} + +pass() +{ + cleanup; + exit 0; +} + +mkdir -p $TEMPDIR + +cd $TEMPDIR + +# Create command file. +activity="create program" +cat > $TESTFILE << EOF +data list free /x y z. +variable alignment x (left)/y (right)/z (center). +variable width x (10)/y (12)/z (14). +variable level x (scale)/y (ordinal)/z (nominal). +display dictionary. +EOF +if [ $? -ne 0 ] ; then no_result ; fi + + +activity="run program" +$SUPERVISOR $PSPP --testing-mode $TESTFILE +if [ $? -ne 0 ] ; then fail ; fi + +activity="compare output" +perl -pi -e 's/^\s*$//g' $TEMPDIR/pspp.list +diff -b $TEMPDIR/pspp.list - << EOF +1.1 DISPLAY. ++--------+-------------------------------------------+--------+ +|Variable|Description |Position| +#========#===========================================#========# +|x |Format: F8.2 | 1| +| |Measure: Scale | | +| |Display Alignment: Left | | +| |Display Width: 10 | | ++--------+-------------------------------------------+--------+ +|y |Format: F8.2 | 2| +| |Measure: Ordinal | | +| |Display Alignment: Right | | +| |Display Width: 12 | | ++--------+-------------------------------------------+--------+ +|z |Format: F8.2 | 3| +| |Measure: Nominal | | +| |Display Alignment: Center | | +| |Display Width: 14 | | ++--------+-------------------------------------------+--------+ +EOF +if [ $? -ne 0 ] ; then fail ; fi + +pass; -- 2.30.2