/* * ***** BEGIN LICENSE BLOCK ***** Copyright (C) 2009-2019 Olof Hagsand and Benny Holmgren This file is part of CLIXON. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Alternatively, the contents of this file may be used under the terms of the GNU General Public License Version 3 or later (the "GPL"), in which case the provisions of the GPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of the GPL, and not to allow others to use your version of this file under the terms of Apache License version 2, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the Apache License version 2 or the GPL. ***** END LICENSE BLOCK ***** * */ #ifdef HAVE_CONFIG_H #include "clixon_config.h" /* generated by config & autoconf */ #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* cligen */ #include /* clicon */ #include #include "clixon_cli_api.h" #include "cli_plugin.h" #include "cli_generate.h" #include "cli_common.h" #include "cli_handle.h" /* Command line options to be passed to getopt(3) */ #define CLI_OPTS "hD:f:l:F:1a:u:d:m:qp:GLy:c:U:o:" /*! Clean and close all state of cli process (but dont exit). * Cannot use h after this * @param[in] h Clixon handle */ static int cli_terminate(clicon_handle h) { yang_spec *yspec; cxobj *x; clicon_rpc_close_session(h); if ((yspec = clicon_dbspec_yang(h)) != NULL) yspec_free(yspec); if ((yspec = clicon_config_yang(h)) != NULL) yspec_free(yspec); if ((x = clicon_conf_xml(h)) != NULL) xml_free(x); cli_plugin_finish(h); cli_handle_exit(h); clicon_log_exit(); return 0; } /*! Unlink pidfile and quit */ static void cli_sig_term(int arg) { clicon_log(LOG_NOTICE, "%s: %u Terminated (killed by sig %d)", __PROGRAM__, getpid(), arg); exit(1); } /*! Setup signal handlers */ static void cli_signal_init (clicon_handle h) { cli_signal_block(h); set_signal(SIGTERM, cli_sig_term, NULL); } /*! Interactive CLI command loop * @param[in] h CLICON handle * @retval 0 * @retval -1 * @see cligen_loop */ static int cli_interactive(clicon_handle h) { int retval = -1; int res; char *cmd; char *new_mode; int eval; /* Loop through all commands */ while(!cligen_exiting(cli_cligen(h))) { new_mode = cli_syntax_mode(h); if ((cmd = clicon_cliread(h)) == NULL) { cligen_exiting_set(cli_cligen(h), 1); /* EOF */ goto done; } if ((res = clicon_parse(h, cmd, &new_mode, &eval)) < 0) goto done; } retval = 0; done: return retval; } static void usage(clicon_handle h, char *argv0) { char *plgdir = clicon_cli_dir(h); fprintf(stderr, "usage:%s [options] [commands]\n" "where commands is a CLI command or options passed to the main plugin\n" "where options are\n" "\t-h \t\tHelp\n" "\t-D \tDebug level\n" "\t-f \tConfig-file (mandatory)\n" "\t-F \tRead commands from file (default stdin)\n" "\t-1\t\tDo not enter interactive mode\n" "\t-a UNIX|IPv4|IPv6\tInternal backend socket family\n" "\t-u \tInternal socket domain path or IP addr (see -a)\n" "\t-d \tSpecify plugin directory (default: %s)\n" "\t-m \tSpecify plugin syntax mode\n" "\t-q \t\tQuiet mode, dont print greetings or prompt, terminate on ctrl-C\n" "\t-p \tYang directory path (see CLICON_YANG_DIR)\n" "\t-G \t\tPrint CLI syntax generated from dbspec (if CLICON_CLI_GENMODEL enabled)\n" "\t-L \t\tDebug print dynamic CLI syntax including completions and expansions\n" "\t-l > \tLog on (s)yslog, std(e)rr, std(o)ut or (f)ile (stderr is default)\n" "\t-y \tOverride yang spec file (dont include .yang suffix)\n" "\t-c \tSpecify cli spec file.\n" "\t-U \tOver-ride unix user with a pseudo user for NACM.\n" "\t-o \"