[Code formatting: Change indentation style to space](https://github.com/clicon/clixon/issues/379)
* Applies to all c/h/y/l/sh files and .editorconfig
This commit is contained in:
parent
a9d1ab006c
commit
d84c529ff1
376 changed files with 38147 additions and 38133 deletions
|
|
@ -72,7 +72,7 @@
|
|||
/*
|
||||
* Constants
|
||||
*/
|
||||
#define CLI_DEFAULT_PROMPT "cli> "
|
||||
#define CLI_DEFAULT_PROMPT "cli> "
|
||||
|
||||
/*
|
||||
*
|
||||
|
|
@ -84,22 +84,22 @@
|
|||
*/
|
||||
static cli_syntaxmode_t *
|
||||
syntax_mode_find(cli_syntax_t *stx,
|
||||
const char *mode,
|
||||
int create)
|
||||
const char *mode,
|
||||
int create)
|
||||
{
|
||||
cli_syntaxmode_t *csm;
|
||||
|
||||
csm = stx->stx_modes;
|
||||
if (csm) {
|
||||
do {
|
||||
if (strcmp(csm->csm_name, mode) == 0)
|
||||
return csm;
|
||||
csm = NEXTQ(cli_syntaxmode_t *, csm);
|
||||
} while (csm && csm != stx->stx_modes);
|
||||
do {
|
||||
if (strcmp(csm->csm_name, mode) == 0)
|
||||
return csm;
|
||||
csm = NEXTQ(cli_syntaxmode_t *, csm);
|
||||
} while (csm && csm != stx->stx_modes);
|
||||
}
|
||||
|
||||
if (create == 0)
|
||||
return NULL;
|
||||
return NULL;
|
||||
|
||||
if ((csm = malloc(sizeof(cli_syntaxmode_t))) == NULL) {
|
||||
clicon_err(OE_UNIX, errno, "malloc");
|
||||
|
|
@ -107,16 +107,16 @@ syntax_mode_find(cli_syntax_t *stx,
|
|||
}
|
||||
memset(csm, 0, sizeof(*csm));
|
||||
if ((csm->csm_name = strdup(mode)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
return NULL;
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
return NULL;
|
||||
}
|
||||
if ((csm->csm_prompt = strdup(CLI_DEFAULT_PROMPT)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
return NULL;
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
return NULL;
|
||||
}
|
||||
if ((csm->csm_pt = pt_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "pt_new");
|
||||
return NULL;
|
||||
clicon_err(OE_UNIX, errno, "pt_new");
|
||||
return NULL;
|
||||
}
|
||||
INSQ(csm, stx->stx_modes);
|
||||
stx->stx_nmodes++;
|
||||
|
|
@ -130,15 +130,15 @@ syntax_mode_find(cli_syntax_t *stx,
|
|||
*/
|
||||
static int
|
||||
gen_parse_tree(clicon_handle h,
|
||||
cli_syntaxmode_t *m)
|
||||
cli_syntaxmode_t *m)
|
||||
{
|
||||
int retval = -1;
|
||||
pt_head *ph;
|
||||
|
||||
if ((ph = cligen_ph_add(cli_cligen(h), m->csm_name)) == NULL)
|
||||
goto done;
|
||||
goto done;
|
||||
if (cligen_ph_parsetree_set(ph, m->csm_pt) < 0)
|
||||
goto done;
|
||||
goto done;
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
|
|
@ -149,17 +149,17 @@ gen_parse_tree(clicon_handle h,
|
|||
*/
|
||||
static int
|
||||
syntax_append(clicon_handle h,
|
||||
cli_syntax_t *stx,
|
||||
const char *name,
|
||||
parse_tree *pt)
|
||||
cli_syntax_t *stx,
|
||||
const char *name,
|
||||
parse_tree *pt)
|
||||
{
|
||||
cli_syntaxmode_t *csm;
|
||||
|
||||
if ((csm = syntax_mode_find(stx, name, 1)) == NULL)
|
||||
return -1;
|
||||
return -1;
|
||||
if (cligen_parsetree_merge(csm->csm_pt, NULL, pt) < 0){
|
||||
clicon_err(OE_PLUGIN, errno, "cligen_parsetree_merge");
|
||||
return -1;
|
||||
clicon_err(OE_PLUGIN, errno, "cligen_parsetree_merge");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -174,19 +174,19 @@ cli_syntax_unload(clicon_handle h)
|
|||
cli_syntaxmode_t *csm;
|
||||
|
||||
if (stx == NULL)
|
||||
return 0;
|
||||
return 0;
|
||||
|
||||
while (stx->stx_nmodes > 0) {
|
||||
csm = stx->stx_modes;
|
||||
DELQ(csm, stx->stx_modes, cli_syntaxmode_t *);
|
||||
if (csm){
|
||||
if (csm->csm_name)
|
||||
free(csm->csm_name);
|
||||
if (csm->csm_prompt)
|
||||
free(csm->csm_prompt);
|
||||
free(csm);
|
||||
}
|
||||
stx->stx_nmodes--;
|
||||
csm = stx->stx_modes;
|
||||
DELQ(csm, stx->stx_modes, cli_syntaxmode_t *);
|
||||
if (csm){
|
||||
if (csm->csm_name)
|
||||
free(csm->csm_name);
|
||||
if (csm->csm_prompt)
|
||||
free(csm->csm_prompt);
|
||||
free(csm);
|
||||
}
|
||||
stx->stx_nmodes--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -208,37 +208,37 @@ cli_syntax_unload(clicon_handle h)
|
|||
*/
|
||||
void *
|
||||
clixon_str2fn(char *name,
|
||||
void *handle,
|
||||
char **error)
|
||||
void *handle,
|
||||
char **error)
|
||||
{
|
||||
void *fn = NULL;
|
||||
|
||||
|
||||
/* Reset error */
|
||||
*error = NULL;
|
||||
/* Special check for auto-cli. If the virtual callback is used, it should be overwritten later
|
||||
* by a callback given in the clispec, eg: set @datamodel, cli_set();
|
||||
*/
|
||||
if (strcmp(name, GENERATE_CALLBACK) == 0)
|
||||
return NULL;
|
||||
return NULL;
|
||||
|
||||
/* First check given plugin if any */
|
||||
if (handle) {
|
||||
dlerror(); /* Clear any existing error */
|
||||
fn = dlsym(handle, name);
|
||||
if ((*error = (char*)dlerror()) == NULL)
|
||||
return fn; /* If no error we found the address of the callback */
|
||||
dlerror(); /* Clear any existing error */
|
||||
fn = dlsym(handle, name);
|
||||
if ((*error = (char*)dlerror()) == NULL)
|
||||
return fn; /* If no error we found the address of the callback */
|
||||
}
|
||||
|
||||
/* Now check global namespace which includes any shared object loaded
|
||||
* into the global namespace. I.e. all lib*.so as well as the
|
||||
* master plugin if it exists
|
||||
*/
|
||||
dlerror(); /* Clear any existing error */
|
||||
dlerror(); /* Clear any existing error */
|
||||
/* RTLD_DEFAULT instead of NULL for linux + FreeBSD:
|
||||
* Use default search algorithm. Thanks jdl@netgate.com */
|
||||
fn = dlsym(RTLD_DEFAULT, name);
|
||||
if ((*error = (char*)dlerror()) == NULL)
|
||||
return fn; /* If no error we found the address of the callback */
|
||||
return fn; /* If no error we found the address of the callback */
|
||||
|
||||
/* Return value not really relevant here as the error string is set to
|
||||
* signal an error. However, just checking the function pointer for NULL
|
||||
|
|
@ -254,16 +254,16 @@ clixon_str2fn(char *name,
|
|||
* Make a lookup of plugins already loaded and resolve callbacks from cligen trees to
|
||||
* dl symbols in the plugin.
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] filename Name of file where syntax is specified (in syntax-group dir)
|
||||
* @param[in] dir Name of dir, or NULL
|
||||
* @param[in] filename Name of file where syntax is specified (in syntax-group dir)
|
||||
* @param[in] dir Name of dir, or NULL
|
||||
* @param[out] ptall Universal CLIgen parse tree: apply to all modes
|
||||
* @see clixon_plugins_load Where .so plugin code has been loaded prior to this
|
||||
*/
|
||||
static int
|
||||
cli_load_syntax_file(clicon_handle h,
|
||||
const char *filename,
|
||||
const char *dir,
|
||||
parse_tree *ptall)
|
||||
const char *filename,
|
||||
const char *dir,
|
||||
parse_tree *ptall)
|
||||
{
|
||||
void *handle = NULL; /* Handle to plugin .so module */
|
||||
char *mode = NULL; /* Name of syntax mode to append new syntax */
|
||||
|
|
@ -281,28 +281,28 @@ cli_load_syntax_file(clicon_handle h,
|
|||
#endif
|
||||
|
||||
if ((pt = pt_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "pt_new");
|
||||
goto done;
|
||||
clicon_err(OE_UNIX, errno, "pt_new");
|
||||
goto done;
|
||||
}
|
||||
if (dir)
|
||||
snprintf(filepath, MAXPATHLEN-1, "%s/%s", dir, filename);
|
||||
snprintf(filepath, MAXPATHLEN-1, "%s/%s", dir, filename);
|
||||
else
|
||||
snprintf(filepath, MAXPATHLEN-1, "%s", filename);
|
||||
snprintf(filepath, MAXPATHLEN-1, "%s", filename);
|
||||
if ((cvv = cvec_new(0)) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "cvec_new");
|
||||
goto done;
|
||||
clicon_err(OE_PLUGIN, errno, "cvec_new");
|
||||
goto done;
|
||||
}
|
||||
/* Build parse tree from syntax spec. */
|
||||
if ((f = fopen(filepath, "r")) == NULL){
|
||||
clicon_err(OE_PLUGIN, errno, "fopen %s", filepath);
|
||||
goto done;
|
||||
clicon_err(OE_PLUGIN, errno, "fopen %s", filepath);
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Assuming this plugin is first in queue */
|
||||
if (cli_parse_file(h, f, filepath, pt, cvv) < 0){
|
||||
clicon_err(OE_PLUGIN, 0, "failed to parse cli file %s", filepath);
|
||||
fclose(f);
|
||||
goto done;
|
||||
clicon_err(OE_PLUGIN, 0, "failed to parse cli file %s", filepath);
|
||||
fclose(f);
|
||||
goto done;
|
||||
}
|
||||
fclose(f);
|
||||
/* Get CLICON specific global variables:
|
||||
|
|
@ -320,70 +320,70 @@ cli_load_syntax_file(clicon_handle h,
|
|||
|
||||
#ifndef CLIXON_STATIC_PLUGINS
|
||||
if (plgnam != NULL) { /* Find plugin for callback resolving */
|
||||
if ((cp = clixon_plugin_find(h, plgnam)) != NULL)
|
||||
handle = clixon_plugin_handle_get(cp);
|
||||
if (handle == NULL){
|
||||
clicon_err(OE_PLUGIN, 0, "CLICON_PLUGIN set to '%s' in %s but plugin %s.so not found in %s",
|
||||
plgnam, filename, plgnam,
|
||||
clicon_cli_dir(h));
|
||||
goto done;
|
||||
}
|
||||
if ((cp = clixon_plugin_find(h, plgnam)) != NULL)
|
||||
handle = clixon_plugin_handle_get(cp);
|
||||
if (handle == NULL){
|
||||
clicon_err(OE_PLUGIN, 0, "CLICON_PLUGIN set to '%s' in %s but plugin %s.so not found in %s",
|
||||
plgnam, filename, plgnam,
|
||||
clicon_cli_dir(h));
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
/* Resolve callback names to function pointers. */
|
||||
if (cligen_callbackv_str2fn(pt, (cgv_str2fn_t*)clixon_str2fn, handle) < 0){
|
||||
clicon_err(OE_PLUGIN, 0, "Mismatch between CLIgen file '%s' and CLI plugin file '%s'. Some possible errors:\n\t1. A function given in the CLIgen file does not exist in the plugin (ie link error)\n\t2. The CLIgen spec does not point to the correct plugin .so file (CLICON_PLUGIN=\"%s\" is wrong)",
|
||||
filename, plgnam, plgnam);
|
||||
goto done;
|
||||
clicon_err(OE_PLUGIN, 0, "Mismatch between CLIgen file '%s' and CLI plugin file '%s'. Some possible errors:\n\t1. A function given in the CLIgen file does not exist in the plugin (ie link error)\n\t2. The CLIgen spec does not point to the correct plugin .so file (CLICON_PLUGIN=\"%s\" is wrong)",
|
||||
filename, plgnam, plgnam);
|
||||
goto done;
|
||||
}
|
||||
if (cligen_expandv_str2fn(pt, (expandv_str2fn_t*)clixon_str2fn, handle) < 0)
|
||||
goto done;
|
||||
goto done;
|
||||
/* Variable translation functions */
|
||||
if (cligen_translate_str2fn(pt, (translate_str2fn_t*)clixon_str2fn, handle) < 0)
|
||||
goto done;
|
||||
goto done;
|
||||
|
||||
/* Make sure we have a syntax mode specified */
|
||||
if (mode == NULL || strlen(mode) < 1) { /* may be null if not given in file */
|
||||
mode = clicon_cli_mode(h);
|
||||
if (mode == NULL || strlen(mode) < 1) { /* may be null if not given in file */
|
||||
clicon_err(OE_PLUGIN, 0, "No syntax mode specified in %s", filepath);
|
||||
goto done;
|
||||
}
|
||||
mode = clicon_cli_mode(h);
|
||||
if (mode == NULL || strlen(mode) < 1) { /* may be null if not given in file */
|
||||
clicon_err(OE_PLUGIN, 0, "No syntax mode specified in %s", filepath);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
/* Find all modes in CLICON_MODE string: where to append the pt syntax tree */
|
||||
if ((vec = clicon_strsep(mode, ":", &nvec)) == NULL)
|
||||
goto done;
|
||||
goto done;
|
||||
|
||||
if (nvec == 1 && strcmp(vec[0], "*") == 0){
|
||||
/* Special case: Add this to all modes. Add to special "universal" syntax
|
||||
* and add to all syntaxes after all files have been loaded. At this point
|
||||
* all modes may not be known (not yet loaded)
|
||||
*/
|
||||
if (cligen_parsetree_merge(ptall, NULL, pt) < 0){
|
||||
clicon_err(OE_PLUGIN, errno, "cligen_parsetree_merge");
|
||||
goto done;
|
||||
}
|
||||
/* Special case: Add this to all modes. Add to special "universal" syntax
|
||||
* and add to all syntaxes after all files have been loaded. At this point
|
||||
* all modes may not be known (not yet loaded)
|
||||
*/
|
||||
if (cligen_parsetree_merge(ptall, NULL, pt) < 0){
|
||||
clicon_err(OE_PLUGIN, errno, "cligen_parsetree_merge");
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (i = 0; i < nvec; i++) {
|
||||
if (syntax_append(h,
|
||||
cli_syntax(h),
|
||||
vec[i],
|
||||
pt) < 0) {
|
||||
goto done;
|
||||
}
|
||||
if (prompt)
|
||||
cli_set_prompt(h, vec[i], prompt);
|
||||
}
|
||||
for (i = 0; i < nvec; i++) {
|
||||
if (syntax_append(h,
|
||||
cli_syntax(h),
|
||||
vec[i],
|
||||
pt) < 0) {
|
||||
goto done;
|
||||
}
|
||||
if (prompt)
|
||||
cli_set_prompt(h, vec[i], prompt);
|
||||
}
|
||||
}
|
||||
|
||||
cligen_parsetree_free(pt, 1);
|
||||
retval = 0;
|
||||
done:
|
||||
if (cvv)
|
||||
cvec_free(cvv);
|
||||
cvec_free(cvv);
|
||||
if (vec)
|
||||
free(vec);
|
||||
free(vec);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
@ -412,10 +412,10 @@ cli_syntax_load(clicon_handle h)
|
|||
|
||||
/* Syntax already loaded. XXX should we re-load?? */
|
||||
if ((stx = cli_syntax(h)) != NULL)
|
||||
return 0;
|
||||
return 0;
|
||||
if ((ptall = pt_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "pt_new");
|
||||
goto done;
|
||||
clicon_err(OE_UNIX, errno, "pt_new");
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Format plugin directory path */
|
||||
|
|
@ -424,36 +424,36 @@ cli_syntax_load(clicon_handle h)
|
|||
|
||||
/* Allocate plugin group object */
|
||||
if ((stx = malloc(sizeof(*stx))) == NULL) {
|
||||
clicon_err(OE_UNIX, errno, "malloc");
|
||||
goto done;
|
||||
clicon_err(OE_UNIX, errno, "malloc");
|
||||
goto done;
|
||||
}
|
||||
memset(stx, 0, sizeof(*stx)); /* Zero out all */
|
||||
memset(stx, 0, sizeof(*stx)); /* Zero out all */
|
||||
|
||||
cli_syntax_set(h, stx);
|
||||
|
||||
/* Load single specific clispec file */
|
||||
if (clispec_file){
|
||||
if (cli_load_syntax_file(h, clispec_file, NULL, ptall) < 0)
|
||||
goto done;
|
||||
if (cli_load_syntax_file(h, clispec_file, NULL, ptall) < 0)
|
||||
goto done;
|
||||
}
|
||||
/* Load all clispec .cli files in directory */
|
||||
if (clispec_dir){
|
||||
/* Get directory list of files */
|
||||
if ((ndp = clicon_file_dirent(clispec_dir, &dp, "(.cli)$", S_IFREG)) < 0)
|
||||
goto done;
|
||||
/* Load the syntax parse trees into cli_syntax stx structure */
|
||||
for (i = 0; i < ndp; i++) {
|
||||
clicon_debug(1, "DEBUG: Loading syntax '%.*s'",
|
||||
(int)strlen(dp[i].d_name)-4, dp[i].d_name);
|
||||
if (cli_load_syntax_file(h, dp[i].d_name, clispec_dir, ptall) < 0)
|
||||
goto done;
|
||||
}
|
||||
/* Get directory list of files */
|
||||
if ((ndp = clicon_file_dirent(clispec_dir, &dp, "(.cli)$", S_IFREG)) < 0)
|
||||
goto done;
|
||||
/* Load the syntax parse trees into cli_syntax stx structure */
|
||||
for (i = 0; i < ndp; i++) {
|
||||
clicon_debug(1, "DEBUG: Loading syntax '%.*s'",
|
||||
(int)strlen(dp[i].d_name)-4, dp[i].d_name);
|
||||
if (cli_load_syntax_file(h, dp[i].d_name, clispec_dir, ptall) < 0)
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
/* Were any syntax modes successfully loaded? If not, leave */
|
||||
if (stx->stx_nmodes <= 0) {
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Go thorugh all modes and :
|
||||
* 1) Add the universal syntax
|
||||
|
|
@ -461,36 +461,36 @@ cli_syntax_load(clicon_handle h)
|
|||
*/
|
||||
m = stx->stx_modes;
|
||||
do {
|
||||
if (cligen_parsetree_merge(m->csm_pt, NULL, ptall) < 0){
|
||||
clicon_err(OE_PLUGIN, errno, "cligen_parsetree_merge");
|
||||
goto done;
|
||||
}
|
||||
if (gen_parse_tree(h, m) != 0)
|
||||
goto done;
|
||||
m = NEXTQ(cli_syntaxmode_t *, m);
|
||||
if (cligen_parsetree_merge(m->csm_pt, NULL, ptall) < 0){
|
||||
clicon_err(OE_PLUGIN, errno, "cligen_parsetree_merge");
|
||||
goto done;
|
||||
}
|
||||
if (gen_parse_tree(h, m) != 0)
|
||||
goto done;
|
||||
m = NEXTQ(cli_syntaxmode_t *, m);
|
||||
} while (m && m != stx->stx_modes);
|
||||
|
||||
/* Set susp and interrupt callbacks into CLIgen */
|
||||
cp = NULL;
|
||||
while ((cp = clixon_plugin_each(h, cp)) != NULL) {
|
||||
if (fns==NULL && (fns = clixon_plugin_api_get(cp)->ca_suspend) != NULL)
|
||||
if (cli_susp_hook(h, fns) < 0)
|
||||
goto done;
|
||||
if (fni==NULL && (fni = clixon_plugin_api_get(cp)->ca_interrupt) != NULL)
|
||||
if (cli_interrupt_hook(h, fni) < 0)
|
||||
goto done;
|
||||
if (fns==NULL && (fns = clixon_plugin_api_get(cp)->ca_suspend) != NULL)
|
||||
if (cli_susp_hook(h, fns) < 0)
|
||||
goto done;
|
||||
if (fni==NULL && (fni = clixon_plugin_api_get(cp)->ca_interrupt) != NULL)
|
||||
if (cli_interrupt_hook(h, fni) < 0)
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* All good. We can now proudly return a new group */
|
||||
retval = 0;
|
||||
done:
|
||||
if (retval != 0) {
|
||||
cli_syntax_unload(h);
|
||||
cli_syntax_set(h, NULL);
|
||||
cli_syntax_unload(h);
|
||||
cli_syntax_set(h, NULL);
|
||||
}
|
||||
cligen_parsetree_free(ptall, 1);
|
||||
if (dp)
|
||||
free(dp);
|
||||
free(dp);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
@ -517,14 +517,14 @@ int
|
|||
cli_handler_err(FILE *f)
|
||||
{
|
||||
if (clicon_errno &&
|
||||
(clicon_get_logflags() & CLICON_LOG_STDERR) == 0){
|
||||
fprintf(f, "%s: %s", clicon_strerror(clicon_errno), clicon_err_reason);
|
||||
if (clicon_suberrno)
|
||||
fprintf(f, ": %s", strerror(clicon_suberrno));
|
||||
fprintf(f, "\n");
|
||||
(clicon_get_logflags() & CLICON_LOG_STDERR) == 0){
|
||||
fprintf(f, "%s: %s", clicon_strerror(clicon_errno), clicon_err_reason);
|
||||
if (clicon_suberrno)
|
||||
fprintf(f, ": %s", strerror(clicon_suberrno));
|
||||
fprintf(f, "\n");
|
||||
}
|
||||
else
|
||||
fprintf(f, "CLI command error\n");
|
||||
fprintf(f, "CLI command error\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -537,7 +537,7 @@ cli_handler_err(FILE *f)
|
|||
* the new mode string.
|
||||
*
|
||||
* @param[in] h Clicon handle
|
||||
* @param[in] cmd Command string
|
||||
* @param[in] cmd Command string
|
||||
* @param[in,out] modenamep Pointer to the mode string pointer
|
||||
* @param[out] result CLIgen match result, < 0: errors, >=0 number of matches
|
||||
* @param[out] evalres Evaluation result if result=1
|
||||
|
|
@ -546,10 +546,10 @@ cli_handler_err(FILE *f)
|
|||
*/
|
||||
int
|
||||
clicon_parse(clicon_handle h,
|
||||
char *cmd,
|
||||
char **modenamep,
|
||||
cligen_result *result,
|
||||
int *evalres)
|
||||
char *cmd,
|
||||
char **modenamep,
|
||||
cligen_result *result,
|
||||
int *evalres)
|
||||
{
|
||||
int retval = -1;
|
||||
char *modename;
|
||||
|
|
@ -566,79 +566,79 @@ clicon_parse(clicon_handle h,
|
|||
|
||||
ch = cli_cligen(h);
|
||||
if (clicon_get_logflags()&CLICON_LOG_STDOUT)
|
||||
f = stdout;
|
||||
f = stdout;
|
||||
else
|
||||
f = stderr;
|
||||
f = stderr;
|
||||
stx = cli_syntax(h);
|
||||
if ((modename = *modenamep) == NULL) {
|
||||
csm = stx->stx_active_mode;
|
||||
modename = csm->csm_name;
|
||||
csm = stx->stx_active_mode;
|
||||
modename = csm->csm_name;
|
||||
}
|
||||
else {
|
||||
if ((csm = syntax_mode_find(stx, modename, 0)) == NULL) {
|
||||
fprintf(f, "Can't find syntax mode '%s'\n", modename);
|
||||
goto done;
|
||||
}
|
||||
if ((csm = syntax_mode_find(stx, modename, 0)) == NULL) {
|
||||
fprintf(f, "Can't find syntax mode '%s'\n", modename);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
if (csm != NULL){
|
||||
if (cligen_ph_active_set_byname(ch, modename) < 0){
|
||||
fprintf(stderr, "No such parse-tree registered: %s\n", modename);
|
||||
goto done;
|
||||
}
|
||||
if ((pt = cligen_pt_active_get(ch)) == NULL){
|
||||
fprintf(stderr, "No such parse-tree registered: %s\n", modename);
|
||||
goto done;
|
||||
}
|
||||
if (cliread_parse(ch, cmd, pt, &match_obj, &cvv, &callbacks, result, &reason) < 0)
|
||||
goto done;
|
||||
/* Debug command and result code */
|
||||
clicon_debug(1, "%s result:%d command: \"%s\"", __FUNCTION__, *result, cmd);
|
||||
switch (*result) {
|
||||
case CG_EOF: /* eof */
|
||||
case CG_ERROR:
|
||||
fprintf(f, "CLI parse error: %s\n", cmd);
|
||||
break;
|
||||
case CG_NOMATCH: /* no match */
|
||||
fprintf(f, "CLI syntax error: \"%s\": %s\n", cmd, reason);
|
||||
break;
|
||||
case CG_MATCH:
|
||||
if (strcmp(modename, *modenamep)){ /* Command in different mode */
|
||||
*modenamep = modename;
|
||||
cli_set_syntax_mode(h, modename);
|
||||
}
|
||||
cli_output_reset();
|
||||
if (!cligen_exiting(ch)) {
|
||||
clicon_err_reset();
|
||||
if ((ret = cligen_eval(ch, match_obj, cvv, callbacks)) < 0) {
|
||||
cli_handler_err(stdout);
|
||||
if (clicon_suberrno == ESHUTDOWN)
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else
|
||||
ret = 0;
|
||||
if (evalres)
|
||||
*evalres = ret;
|
||||
break;
|
||||
default:
|
||||
fprintf(f, "CLI syntax error: \"%s\" is ambiguous\n", cmd);
|
||||
break;
|
||||
} /* switch result */
|
||||
if (cvv){
|
||||
cvec_free(cvv);
|
||||
cvv = NULL;
|
||||
}
|
||||
if (cligen_ph_active_set_byname(ch, modename) < 0){
|
||||
fprintf(stderr, "No such parse-tree registered: %s\n", modename);
|
||||
goto done;
|
||||
}
|
||||
if ((pt = cligen_pt_active_get(ch)) == NULL){
|
||||
fprintf(stderr, "No such parse-tree registered: %s\n", modename);
|
||||
goto done;
|
||||
}
|
||||
if (cliread_parse(ch, cmd, pt, &match_obj, &cvv, &callbacks, result, &reason) < 0)
|
||||
goto done;
|
||||
/* Debug command and result code */
|
||||
clicon_debug(1, "%s result:%d command: \"%s\"", __FUNCTION__, *result, cmd);
|
||||
switch (*result) {
|
||||
case CG_EOF: /* eof */
|
||||
case CG_ERROR:
|
||||
fprintf(f, "CLI parse error: %s\n", cmd);
|
||||
break;
|
||||
case CG_NOMATCH: /* no match */
|
||||
fprintf(f, "CLI syntax error: \"%s\": %s\n", cmd, reason);
|
||||
break;
|
||||
case CG_MATCH:
|
||||
if (strcmp(modename, *modenamep)){ /* Command in different mode */
|
||||
*modenamep = modename;
|
||||
cli_set_syntax_mode(h, modename);
|
||||
}
|
||||
cli_output_reset();
|
||||
if (!cligen_exiting(ch)) {
|
||||
clicon_err_reset();
|
||||
if ((ret = cligen_eval(ch, match_obj, cvv, callbacks)) < 0) {
|
||||
cli_handler_err(stdout);
|
||||
if (clicon_suberrno == ESHUTDOWN)
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
else
|
||||
ret = 0;
|
||||
if (evalres)
|
||||
*evalres = ret;
|
||||
break;
|
||||
default:
|
||||
fprintf(f, "CLI syntax error: \"%s\" is ambiguous\n", cmd);
|
||||
break;
|
||||
} /* switch result */
|
||||
if (cvv){
|
||||
cvec_free(cvv);
|
||||
cvv = NULL;
|
||||
}
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
if (callbacks)
|
||||
co_callbacks_free(&callbacks);
|
||||
co_callbacks_free(&callbacks);
|
||||
if (reason)
|
||||
free(reason);
|
||||
free(reason);
|
||||
if (cvv)
|
||||
cvec_free(cvv);
|
||||
cvec_free(cvv);
|
||||
if (match_obj)
|
||||
co_free(match_obj, 0);
|
||||
co_free(match_obj, 0);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
@ -650,7 +650,7 @@ done:
|
|||
*/
|
||||
static char *
|
||||
cli_prompt_get(clicon_handle h,
|
||||
char *fmt)
|
||||
char *fmt)
|
||||
{
|
||||
char *s = fmt;
|
||||
char hname[1024];
|
||||
|
|
@ -662,79 +662,79 @@ cli_prompt_get(clicon_handle h,
|
|||
char *str0;
|
||||
|
||||
if ((cb = cbuf_new()) == NULL){
|
||||
clicon_err(OE_XML, errno, "cbuf_new");
|
||||
goto done;
|
||||
clicon_err(OE_XML, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
/* Start with empty string */
|
||||
while(*s) {
|
||||
if (*s == '%' && *++s) {
|
||||
switch(*s) {
|
||||
case 'H': /* Hostname */
|
||||
if (gethostname(hname, sizeof(hname)) != 0)
|
||||
strncpy(hname, "unknown", sizeof(hname)-1);
|
||||
cprintf(cb, "%s", hname);
|
||||
break;
|
||||
case 'U': /* Username */
|
||||
tmp = getenv("USER");
|
||||
cprintf(cb, "%s", tmp?tmp:"nobody");
|
||||
break;
|
||||
case 'T': /* TTY */
|
||||
if(ttyname_r(fileno(stdin), tty, sizeof(tty)-1) < 0)
|
||||
strcpy(tty, "notty");
|
||||
cprintf(cb, "%s", tty);
|
||||
break;
|
||||
case 'W': /* Last element of working path */
|
||||
if (clicon_data_get(h, "cli-edit-mode", &path) == 0 &&
|
||||
strlen(path)){
|
||||
int i;
|
||||
if (*s == '%' && *++s) {
|
||||
switch(*s) {
|
||||
case 'H': /* Hostname */
|
||||
if (gethostname(hname, sizeof(hname)) != 0)
|
||||
strncpy(hname, "unknown", sizeof(hname)-1);
|
||||
cprintf(cb, "%s", hname);
|
||||
break;
|
||||
case 'U': /* Username */
|
||||
tmp = getenv("USER");
|
||||
cprintf(cb, "%s", tmp?tmp:"nobody");
|
||||
break;
|
||||
case 'T': /* TTY */
|
||||
if(ttyname_r(fileno(stdin), tty, sizeof(tty)-1) < 0)
|
||||
strcpy(tty, "notty");
|
||||
cprintf(cb, "%s", tty);
|
||||
break;
|
||||
case 'W': /* Last element of working path */
|
||||
if (clicon_data_get(h, "cli-edit-mode", &path) == 0 &&
|
||||
strlen(path)){
|
||||
int i;
|
||||
|
||||
for (i=strlen(path)-1; i>=0; i--)
|
||||
if (path[i] == '/' || path[i] == ':')
|
||||
/* see yang2api_path_fmt_1() why occasional trailing / */
|
||||
if (i < strlen(path)-1)
|
||||
break;
|
||||
if (i >= 0)
|
||||
cprintf(cb, "%s", &path[i+1]);
|
||||
else
|
||||
cprintf(cb, "%s", path);
|
||||
}
|
||||
else
|
||||
cprintf(cb, "/");
|
||||
break;
|
||||
case 'w': /* Full Working edit path */
|
||||
if (clicon_data_get(h, "cli-edit-mode", &path) == 0 &&
|
||||
strlen(path))
|
||||
cprintf(cb, "%s", path);
|
||||
else
|
||||
cprintf(cb, "/");
|
||||
break;
|
||||
default:
|
||||
cprintf(cb, "%%");
|
||||
cprintf(cb, "%c", *s);
|
||||
}
|
||||
}
|
||||
else if (*s == '\\' && *++s) {
|
||||
switch(*s) {
|
||||
case 'n':
|
||||
cprintf(cb, "\n");
|
||||
break;
|
||||
default:
|
||||
cprintf(cb, "\\");
|
||||
cprintf(cb, "%c", *s);
|
||||
}
|
||||
}
|
||||
else
|
||||
cprintf(cb, "%c", *s);
|
||||
s++;
|
||||
for (i=strlen(path)-1; i>=0; i--)
|
||||
if (path[i] == '/' || path[i] == ':')
|
||||
/* see yang2api_path_fmt_1() why occasional trailing / */
|
||||
if (i < strlen(path)-1)
|
||||
break;
|
||||
if (i >= 0)
|
||||
cprintf(cb, "%s", &path[i+1]);
|
||||
else
|
||||
cprintf(cb, "%s", path);
|
||||
}
|
||||
else
|
||||
cprintf(cb, "/");
|
||||
break;
|
||||
case 'w': /* Full Working edit path */
|
||||
if (clicon_data_get(h, "cli-edit-mode", &path) == 0 &&
|
||||
strlen(path))
|
||||
cprintf(cb, "%s", path);
|
||||
else
|
||||
cprintf(cb, "/");
|
||||
break;
|
||||
default:
|
||||
cprintf(cb, "%%");
|
||||
cprintf(cb, "%c", *s);
|
||||
}
|
||||
}
|
||||
else if (*s == '\\' && *++s) {
|
||||
switch(*s) {
|
||||
case 'n':
|
||||
cprintf(cb, "\n");
|
||||
break;
|
||||
default:
|
||||
cprintf(cb, "\\");
|
||||
cprintf(cb, "%c", *s);
|
||||
}
|
||||
}
|
||||
else
|
||||
cprintf(cb, "%c", *s);
|
||||
s++;
|
||||
}
|
||||
str0 = cbuf_len(cb) ? cbuf_get(cb) : CLI_DEFAULT_PROMPT;
|
||||
if ((promptstr = strdup(str0)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
goto done;
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
goto done;
|
||||
}
|
||||
done:
|
||||
if (cb)
|
||||
cbuf_free(cb);
|
||||
cbuf_free(cb);
|
||||
return promptstr;
|
||||
}
|
||||
|
||||
|
|
@ -746,7 +746,7 @@ cli_prompt_get(clicon_handle h,
|
|||
*/
|
||||
int
|
||||
clicon_cliread(clicon_handle h,
|
||||
char **stringp)
|
||||
char **stringp)
|
||||
{
|
||||
int retval = -1;
|
||||
char *pfmt = NULL;
|
||||
|
|
@ -761,29 +761,29 @@ clicon_cliread(clicon_handle h,
|
|||
/* Get prompt from plugin callback? */
|
||||
cp = NULL;
|
||||
while ((cp = clixon_plugin_each(h, cp)) != NULL) {
|
||||
if ((fn = clixon_plugin_api_get(cp)->ca_prompt) == NULL)
|
||||
continue;
|
||||
pfmt = fn(h, mode->csm_name);
|
||||
break;
|
||||
if ((fn = clixon_plugin_api_get(cp)->ca_prompt) == NULL)
|
||||
continue;
|
||||
pfmt = fn(h, mode->csm_name);
|
||||
break;
|
||||
}
|
||||
if (clicon_quiet_mode(h))
|
||||
cli_prompt_set(h, "");
|
||||
cli_prompt_set(h, "");
|
||||
else{
|
||||
if ((promptstr = cli_prompt_get(h, pfmt ? pfmt : mode->csm_prompt)) == NULL)
|
||||
goto done;
|
||||
cli_prompt_set(h, promptstr);
|
||||
free(promptstr);
|
||||
if ((promptstr = cli_prompt_get(h, pfmt ? pfmt : mode->csm_prompt)) == NULL)
|
||||
goto done;
|
||||
cli_prompt_set(h, promptstr);
|
||||
free(promptstr);
|
||||
}
|
||||
cligen_ph_active_set_byname(cli_cligen(h), mode->csm_name);
|
||||
|
||||
if (cliread(cli_cligen(h), stringp) < 0){
|
||||
clicon_err(OE_FATAL, errno, "CLIgen");
|
||||
goto done;
|
||||
clicon_err(OE_FATAL, errno, "CLIgen");
|
||||
goto done;
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
if (pfmt)
|
||||
free(pfmt);
|
||||
free(pfmt);
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
|
@ -798,12 +798,12 @@ clicon_cliread(clicon_handle h,
|
|||
*/
|
||||
int
|
||||
cli_set_syntax_mode(clicon_handle h,
|
||||
const char *name)
|
||||
const char *name)
|
||||
{
|
||||
cli_syntaxmode_t *mode;
|
||||
|
||||
if ((mode = syntax_mode_find(cli_syntax(h), name, 1)) == NULL)
|
||||
return 0;
|
||||
return 0;
|
||||
|
||||
cli_syntax(h)->stx_active_mode = mode;
|
||||
return 1;
|
||||
|
|
@ -818,7 +818,7 @@ cli_syntax_mode(clicon_handle h)
|
|||
cli_syntaxmode_t *csm;
|
||||
|
||||
if ((csm = cli_syntax(h)->stx_active_mode) == NULL)
|
||||
return NULL;
|
||||
return NULL;
|
||||
return csm->csm_name;
|
||||
}
|
||||
|
||||
|
|
@ -831,21 +831,21 @@ cli_syntax_mode(clicon_handle h)
|
|||
*/
|
||||
int
|
||||
cli_set_prompt(clicon_handle h,
|
||||
const char *name,
|
||||
const char *prompt)
|
||||
const char *name,
|
||||
const char *prompt)
|
||||
{
|
||||
cli_syntaxmode_t *csm;
|
||||
|
||||
if ((csm = syntax_mode_find(cli_syntax(h), name, 1)) == NULL)
|
||||
return -1;
|
||||
return -1;
|
||||
|
||||
if (csm->csm_prompt != NULL){
|
||||
free(csm->csm_prompt);
|
||||
csm->csm_prompt = NULL;
|
||||
free(csm->csm_prompt);
|
||||
csm->csm_prompt = NULL;
|
||||
}
|
||||
if ((csm->csm_prompt = strdup(prompt)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
return -1;
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue