I noticed some unexpected control characters at the beginning of actual output from parted, and tracked it down to a bug whereby rl_initialize was being called in spite of my use of --script (-s).
Note that this effect disappears if you set e.g., TERM=dumb or unset TERM, as is done in the tests/test-lib.sh, but if you set e.g., TERM=xterm, it would print the control characters. >From bbd1aa6fc0097a04dea53edf8b5688540d6a00bc Mon Sep 17 00:00:00 2001 From: Jim Meyering <[email protected]> Date: Thu, 24 Sep 2009 18:46:45 +0200 Subject: [PATCH] ui: do not initialize readline (which would output!) in --script mode Running parted with its --script (-s) option would, surprisingly, print a few control characters if TERM were set appropriately, and if readline and curses support were compiled in. This fixes it not to do that. * parted/parted.c (_init): Initialize readline support only after parsing command line options, so we can skip it in --script mode. * parted/ui.c (init_readline): New function. Body extracted from ... (init_ui): ...here. * parted/ui.h (init_readline): Declare. * tests/t0010-script-no-ctrl-chars.sh: New file. Test for the above. * tests/Makefile.am (TESTS): Add that new file. --- parted/parted.c | 3 ++ parted/ui.c | 18 ++++++++---- parted/ui.h | 3 +- tests/Makefile.am | 1 + tests/t0010-script-no-ctrl-chars.sh | 50 +++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 7 deletions(-) create mode 100755 tests/t0010-script-no-ctrl-chars.sh diff --git a/parted/parted.c b/parted/parted.c index a7253d2..cde0ae3 100644 --- a/parted/parted.c +++ b/parted/parted.c @@ -1999,6 +1999,9 @@ _init_commands (); if (!_parse_options (argc_ptr, argv_ptr)) goto error_done_commands; +if (!opt_script_mode) + init_readline (); + #ifdef HAVE_GETUID if (getuid() != 0 && !opt_script_mode) { puts (_("WARNING: You are not superuser. Watch out for " diff --git a/parted/ui.c b/parted/ui.c index d61f6ac..3b55024 100644 --- a/parted/ui.c +++ b/parted/ui.c @@ -1394,6 +1394,18 @@ init_disk_type_str () } int +init_readline (void) +{ +#ifdef HAVE_LIBREADLINE + if (!opt_script_mode) { + rl_initialize (); + rl_attempted_completion_function = (CPPFunction*) complete_function; + readline_state.in_readline = 0; + } +#endif +} + +int init_ui () { if (!init_ex_opt_str () @@ -1403,12 +1415,6 @@ init_ui () return 0; ped_exception_set_handler (exception_handler); -#ifdef HAVE_LIBREADLINE - rl_initialize (); - rl_attempted_completion_function = (CPPFunction*) complete_function; - readline_state.in_readline = 0; -#endif - #ifdef SA_SIGINFO sigset_t curr; sigfillset (&curr); diff --git a/parted/ui.h b/parted/ui.h index 77bb194..da3fca5 100644 --- a/parted/ui.h +++ b/parted/ui.h @@ -1,6 +1,6 @@ /* parted - a frontend to libparted - Copyright (C) 1999, 2000, 2001, 2007-2008 Free Software Foundation, Inc. + Copyright (C) 1999, 2000, 2001, 2007-2009 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -24,6 +24,7 @@ extern const char *prog_name; extern int init_ui (); +extern int init_readline (); extern int non_interactive_mode (PedDevice** dev, Command* cmd_list[], int argc, char* argv[]); extern int interactive_mode (PedDevice** dev, Command* cmd_list[]); diff --git a/tests/Makefile.am b/tests/Makefile.am index 326ccb4..548e469 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,5 +1,6 @@ TESTS = \ t0000-basic.sh \ + t0010-script-no-ctrl-chars.sh \ t0100-print.sh \ t0200-gpt.sh \ t0201-gpt.sh \ diff --git a/tests/t0010-script-no-ctrl-chars.sh b/tests/t0010-script-no-ctrl-chars.sh new file mode 100755 index 0000000..9862eea --- /dev/null +++ b/tests/t0010-script-no-ctrl-chars.sh @@ -0,0 +1,50 @@ +#!/bin/sh +# Ensure that printing with -s outputs no readline control chars + +# Copyright (C) 2009 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +test_description='--script does no readline initialization' + +: ${srcdir=.} +. $srcdir/test-lib.sh + +ss=$sector_size_ +n_sectors=5000 +dev=loop-file + +test_expect_success \ + 'create the test file' \ + 'dd if=/dev/null of=$dev bs=$ss seek=$n_sectors' + +test_expect_success \ + 'run parted -s FILE mklabel msdos' \ + 'parted -s $dev mklabel msdos > out 2>&1' +test_expect_success 'expect no output' 'compare out /dev/null' + +test_expect_success \ + 'print partition table in --script mode' \ + 'TERM=xterm parted -m -s $dev u s p > out 2>&1' + +ok=0 +sed "s,.*/$dev:,$dev:," out > k && mv k out && +printf "BYT;\n$dev:${n_sectors}s:file:$ss:$ss:msdos:;\n" > exp && + ok=1 + +test_expect_success \ + 'match against expected output' \ + 'test $ok = 1 && compare out exp' + +test_done -- 1.6.5.rc2.177.ga9dd6 _______________________________________________ parted-devel mailing list [email protected] http://lists.alioth.debian.org/mailman/listinfo/parted-devel

