From 37657313d95fa7735e52328b5427caf0fd3db03d Mon Sep 17 00:00:00 2001 From: An-Cheng Huang Date: Fri, 15 Apr 2011 17:23:28 -0700 Subject: add validation to config file API --- src/cli_shell_api.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/cli_shell_api.cpp b/src/cli_shell_api.cpp index a701a71..3ab4996 100644 --- a/src/cli_shell_api.cpp +++ b/src/cli_shell_api.cpp @@ -471,6 +471,20 @@ loadFile(Cstore& cstore, const Cpath& args) } } +static cnode::CfgNode * +_cf_process_args(Cstore& cstore, const Cpath& args, Cpath& path) +{ + for (size_t i = 1; i < args.size(); i++) { + path.push(args[i]); + } + cnode::CfgNode *root = cparse::parse_file(args[0], cstore); + if (!root) { + fprintf(stderr, "Failed to parse config file\n"); + exit(1); + } + return root; +} + /* the following "cf" functions form the "config file" shell API, which * allows shell scripts to "query" the "config" represented by a config * file in a way similar to how they query the active/working config. @@ -485,10 +499,7 @@ static void cfExists(Cstore& cstore, const Cpath& args) { Cpath path; - for (size_t i = 1; i < args.size(); i++) { - path.push(args[i]); - } - cnode::CfgNode *root = cparse::parse_file(args[0], cstore); + cnode::CfgNode *root = _cf_process_args(cstore, args, path); exit(cnode::findCfgNode(root, path) ? 0 : 1); } @@ -496,10 +507,7 @@ static void cfReturnValue(Cstore& cstore, const Cpath& args) { Cpath path; - for (size_t i = 1; i < args.size(); i++) { - path.push(args[i]); - } - cnode::CfgNode *root = cparse::parse_file(args[0], cstore); + cnode::CfgNode *root = _cf_process_args(cstore, args, path); string value; if (!cnode::getCfgNodeValue(root, path, value)) { exit(1); @@ -511,10 +519,7 @@ static void cfReturnValues(Cstore& cstore, const Cpath& args) { Cpath path; - for (size_t i = 1; i < args.size(); i++) { - path.push(args[i]); - } - cnode::CfgNode *root = cparse::parse_file(args[0], cstore); + cnode::CfgNode *root = _cf_process_args(cstore, args, path); vector values; if (!cnode::getCfgNodeValues(root, path, values)) { exit(1); -- cgit v1.2.3