diff options
author | An-Cheng Huang <ancheng@vyatta.com> | 2011-04-15 13:58:20 -0700 |
---|---|---|
committer | An-Cheng Huang <ancheng@vyatta.com> | 2011-04-15 13:58:20 -0700 |
commit | 74b730a5a95abadba47bd05e816ed8c93abe9ca2 (patch) | |
tree | 4e7b3d790121d2637d807080988cc9cd080f7d2f /src/cli_shell_api.cpp | |
parent | 53f4203e924f9f98347f962408fec532e0a1849f (diff) | |
download | vyatta-cfg-74b730a5a95abadba47bd05e816ed8c93abe9ca2.tar.gz vyatta-cfg-74b730a5a95abadba47bd05e816ed8c93abe9ca2.zip |
initial implementation of "config file" shell API
* this API allows shell scripts to "query" the "config" represented by a config file in a way similar to how they query the active/working config.
Diffstat (limited to 'src/cli_shell_api.cpp')
-rw-r--r-- | src/cli_shell_api.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/cli_shell_api.cpp b/src/cli_shell_api.cpp index 363a38f..a701a71 100644 --- a/src/cli_shell_api.cpp +++ b/src/cli_shell_api.cpp @@ -471,6 +471,57 @@ loadFile(Cstore& cstore, const Cpath& args) } } +/* 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. + * usage example: + * + * cli-shell-api cfExists /config/config.boot service ssh allow-root + * + * the above command will exit with 0 (success) if the "allow-root" node + * is present in the specified config file (or exit with 1 if it's not). + */ +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); + exit(cnode::findCfgNode(root, path) ? 0 : 1); +} + +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); + string value; + if (!cnode::getCfgNodeValue(root, path, value)) { + exit(1); + } + printf("%s", value.c_str()); +} + +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); + vector<string> values; + if (!cnode::getCfgNodeValues(root, path, values)) { + exit(1); + } + print_vec(values, " ", "'"); +} + #define OP(name, exact, exact_err, min, min_err, use_edit) \ { #name, exact, exact_err, min, min_err, use_edit, &name } @@ -517,6 +568,10 @@ static OpT ops[] = { OP(showConfig, -1, NULL, -1, NULL, true), OP(loadFile, 1, "Must specify config file", -1, NULL, false), + OP(cfExists, -1, NULL, 2, "Must specify config file and path", false), + OP(cfReturnValue, -1, NULL, 2, "Must specify config file and path", false), + OP(cfReturnValues, -1, NULL, 2, "Must specify config file and path", false), + {NULL, -1, NULL, -1, NULL, NULL, false} }; #define OP_exact_args ops[op_idx].op_exact_args |