From 094e447f93567dc6bf13f5865f80a3b0327730e1 Mon Sep 17 00:00:00 2001 From: wangchao Date: Wed, 28 Jan 2004 02:06:05 +0000 Subject: [PATCH] b=2323 r=shaver Deal with the double-quote,single-quote, and backslash in batchfile of "lmc --batch batchfile". --- lustre/tests/conf-sanity.sh | 71 +++++++++++++++++++++++++++++++++++++++++++++ lustre/utils/lmc | 63 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 132 insertions(+), 2 deletions(-) diff --git a/lustre/tests/conf-sanity.sh b/lustre/tests/conf-sanity.sh index 2b2e1ef..6c3978e 100644 --- a/lustre/tests/conf-sanity.sh +++ b/lustre/tests/conf-sanity.sh @@ -314,4 +314,75 @@ test_11() { } run_test 11 "use default lov configuration (should return error)" +test_12() { + OLDXMLCONFIG=$XMLCONFIG + XMLCONFIG="batch.xml" + BATCHFILE="batchfile" + + # test double quote + [ -f "$XMLCONFIG" ] && rm -f $XMLCONFIG + [ -f "$BATCHFILE" ] && rm -f $BATCHFILE + echo "--add net --node localhost --nid localhost.localdomain --nettype tcp" > $BATCHFILE + echo "--add mds --node localhost --mds mds1 --mkfsoptions \"-I 128\"" >> $BATCHFILE + # --mkfsoptions "-I 128" + do_lmc -m $XMLCONFIG --batch $BATCHFILE || return $? + if [ `sed -n '/>-I 128 $BATCHFILE + echo "--add mds --node localhost --mds mds1 --mkfsoptions \"-I 128" >> $BATCHFILE + # --mkfsoptions "-I 128 + do_lmc -m $XMLCONFIG --batch $BATCHFILE && return $? + echo "unmatched double quote should return error" + + # test single quote + rm -f $BATCHFILE + echo "--add net --node localhost --nid localhost.localdomain --nettype tcp" > $BATCHFILE + echo "--add mds --node localhost --mds mds1 --mkfsoptions '-I 128'" >> $BATCHFILE + # --mkfsoptions '-I 128' + do_lmc -m $XMLCONFIG --batch $BATCHFILE || return $? + if [ `sed -n '/>-I 128 $BATCHFILE + echo "--add mds --node localhost --mds mds1 --mkfsoptions '-I 128" >> $BATCHFILE + # --mkfsoptions '-I 128 + do_lmc -m $XMLCONFIG --batch $BATCHFILE && return $? + echo "unmatched single quote should return error" + + # test backslash + rm -f $BATCHFILE + echo "--add net --node localhost --nid localhost.localdomain --nettype tcp" > $BATCHFILE + echo "--add mds --node localhost --mds mds1 --mkfsoptions \-\I\ \128" >> $BATCHFILE + # --mkfsoptions \-\I\ \128 + do_lmc -m $XMLCONFIG --batch $BATCHFILE || return $? + if [ `sed -n '/>-I 128 $BATCHFILE + echo "--add mds --node localhost --mds mds1 --mkfsoptions -I\ 128\\" >> $BATCHFILE + # --mkfsoptions -I\ 128\ + do_lmc -m $XMLCONFIG --batch $BATCHFILE && return $? + echo "backslash followed by nothing should return error" + + rm -f $BATCHFILE + XMLCONFIG=$OLDXMLCONFIG +} +run_test 12 "lmc --batch, with single/double quote, backslash in batchfile" + equals_msg "Done" diff --git a/lustre/utils/lmc b/lustre/utils/lmc index d668848..9854c64 100755 --- a/lustre/utils/lmc +++ b/lustre/utils/lmc @@ -25,7 +25,7 @@ lmc - lustre configuration data manager """ -import sys, os, getopt, string, exceptions, random +import sys, os, getopt, string, exceptions, random, re import xml.dom.minidom from xml.dom.ext import PrettyPrint @@ -993,6 +993,65 @@ class chrono: str = '%s: %g secs' % (msg, d) print str +################################################################# +# function cmdlinesplit used to split cmd line from batch file +# +def cmdlinesplit(cmdline): + + double_quote = re.compile(r'"(([^"\\]|\\.)*)"') + single_quote = re.compile(r"'(.*?)'") + escaped = re.compile(r'\\(.)') + esc_quote = re.compile(r'\\([\\"])') + outside = re.compile(r"""([^\s\\'"]+)""") + + arg_list = [] + i = 0; arg = None + while i < len(cmdline): + c = cmdline[i] + if c == '"': + match = double_quote.match(cmdline, i) + if not match: + print "Unmatched double quote:", cmdline + sys.exit(1) + i = match.end() + if arg is None: arg = esc_quote.sub(r'\1', match.group(1)) + else: arg += esc_quote.sub(r'\1', match.group(1)) + + elif c == "'": + match = single_quote.match(cmdline, i) + if not match: + print "Unmatched single quote:", cmdline + sys.exit(1) + i = match.end() + if arg is None: arg = match.group(1) + else: arg += match.group(1) + + elif c == "\\": + match = escaped.match(cmdline, i) + if not match: + print "Unmatched backslash", cmdline + sys.exit(1) + i = match.end() + if arg is None: arg = match.group(1) + else: arg += match.group(1) + + elif c.isspace(): + if arg != None: + arg_list.append(str(arg)) + arg = None + while i < len(cmdline) and cmdline[i].isspace(): + i += 1 + else: + match = outside.match(cmdline, i) + assert match + i = match.end() + if arg is None: arg = match.group() + else: arg += match.group() + + if arg != None: arg_list.append(str(arg)) + + return arg_list + ############################################################ # Main # @@ -1080,7 +1139,7 @@ def main(): fp.close() for cmd in batchCommands: try: - options, args = cl.parse(string.split(cmd)) + options, args = cl.parse(cmdlinesplit(cmd)) if options.merge or options.input or options.output: print "The batchfile should not contain --merge, --input or --output." sys.exit(1) -- 1.8.3.1