コード例 #1
0
/********************************************************************
 * FUNCTION save_aliases
 * 
 * Save the aliases to the specified filespec
 *
 * INPUT:
 *   fspec == output filespec to use  (NULL == default)
 *
 * RETURNS:
 *   status
 *********************************************************************/
status_t
    save_aliases (const xmlChar *fspec)
{
    if (fspec == NULL) {
        fspec = get_aliases_file();
    }

    status_t res = NO_ERR;
    xmlChar *fullspec = ncx_get_source(fspec, &res);
    if (res != NO_ERR) {
        log_error("\nError: Expand source '%s' failed (%s)\n",
                  fspec, get_error_string(res));
        return res;
    }

    res = check_for_saving_def_yangcli_file (ALIASES_FILE, fullspec);
    if (res != NO_ERR) {
        m__free(fullspec);
        return res;
    }

    FILE *fp = fopen((const char *)fullspec, "w");
    if (fp) {
        /* this will truncate the file if there are no aliases */
        alias_cb_t  *alias;
        for (alias = get_first_alias();
             alias != NULL;
             alias = get_next_alias(alias)) {
            write_alias(fp, alias);
        }

        /* Save mtime of this aliases file */
        res = update_def_yangcli_file_mtime (ALIASES_FILE, fullspec);
        fclose(fp);
    } else {
        res = errno_to_status();
        log_error("\nError: Open aliases file '%s' failed (%s)\n",
                  fullspec, get_error_string(res));

    }

    m__free(fullspec);

    return res;

}  /* save_aliases */
コード例 #2
0
ファイル: conf.c プロジェクト: 0xDEC0DE8/OpenYuma
/********************************************************************
* FUNCTION conf_parse_from_filespec
* 
* Parse a file as an NCX text config file against
* a specific parmset definition.  Fill in an
* initialized parmset (and could be partially filled in)
*
* Error messages are printed by this function!!
*
* If a value is already set, and only one value is allowed
* then the 'keepvals' parameter will control whether that
* value will be kept or overwitten
*
* INPUTS:
*   filespec == absolute path or relative path
*               This string is used as-is without adjustment.
*   val       == value struct to fill in, must be initialized
*                already with val_new_value or val_init_value
*   keepvals == TRUE if old values should always be kept
*               FALSE if old vals should be overwritten
*   fileerr == TRUE to generate a missing file error
*              FALSE to return NO_ERR instead, if file not found
*
* RETURNS:
*   status of the operation
*********************************************************************/
status_t 
    conf_parse_val_from_filespec (const xmlChar *filespec,
                                  val_value_t *val,
                                  boolean keepvals,
                                  boolean fileerr)
{
    tk_chain_t    *tkc;
    FILE          *fp;
    xmlChar       *sourcespec;
    status_t       res;

#ifdef DEBUG
    if (!filespec || !val) {
        return SET_ERROR(ERR_INTERNAL_PTR);
    } 
#endif

    if (*filespec == 0) {
        log_error("\nError: no config file name specified");
        return ERR_NCX_INVALID_VALUE;
    }
        
    res = NO_ERR;
    sourcespec = ncx_get_source(filespec, &res);
    if (!sourcespec) {
        return res;
    }

    fp = fopen((const char *)sourcespec, "r");

    if (!fp) {
        m__free(sourcespec);
        if (fileerr) {
            log_error("\nError: config file '%s' could not be opened",
                      filespec);
            return ERR_FIL_OPEN;
        } else {
            return NO_ERR;
        }
    }

    if (LOGINFO) {
        log_info("\nLoading CLI parameters from '%s'", sourcespec);
    }

    m__free(sourcespec);
    sourcespec = NULL;

    /* get a new token chain */
    res = NO_ERR;
    tkc = tk_new_chain();
    if (!tkc) {
        res = ERR_INTERNAL_MEM;
        ncx_print_errormsg(NULL, NULL, res);
        fclose(fp);
        return res;
    }

    /* else setup the token chain and parse this config file */
    tk_setup_chain_conf(tkc, fp, filespec);

    res = tk_tokenize_input(tkc, NULL);

#ifdef CONF_TK_DEBUG
    if (LOGDEBUG3) {
        tk_dump_chain(tkc);
    }
#endif

    if (res == NO_ERR) {
        res = parse_top(tkc, val, keepvals);
    }

    fclose(fp);
    tkc->fp = NULL;
    tk_free_chain(tkc);

    if (res != NO_ERR) {
        log_error("\nError: invalid .conf file '%s' (%s)",
                  filespec,
                  get_error_string(res));
    }

    return res;

}  /* conf_parse_val_from_filespec */
コード例 #3
0
/********************************************************************
 * FUNCTION load_aliases
 * 
 * Load the aliases from the specified filespec
 *
 * INPUT:
 *   fspec == input filespec to use (NULL == default)
 *   file_error == TRUE if the file must be found
 *                 FALSE if finding the default file and it
 *                 is not required to be present
 * RETURNS:
 *   status
 *********************************************************************/
status_t
    load_aliases (const xmlChar *fspec,
                  boolean file_error)
{
    FILE      *fp;
    xmlChar   *fullspec, *buffer;
    status_t   res = NO_ERR;
    
    buffer = m__getMem(NCX_MAX_LINELEN+1);
    if (buffer == NULL) {
        log_error("\nError: malloc failed\n");
        return ERR_INTERNAL_MEM;
    }

    if (fspec == NULL) {
        fspec = get_aliases_file();
    }

    fullspec = ncx_get_source(fspec, &res);
    if (res == NO_ERR && fullspec) {
        fp = fopen((const char *)fullspec, "r");
        if (fp) {
#define MAX_ALIAS_ERRORS 5
            boolean done = FALSE;
            uint32 errorcnt = 0;
            while (!done) {
                char *retstr = fgets((char *)buffer, NCX_MAX_LINELEN+1, fp);
                if (retstr) {
                    uint32 len = xml_strlen(buffer);
                    if (len) {
                        if (*buffer != '#' && *buffer != '\n') {
                            if (buffer[len-1] == '\n') {
                                buffer[len-1] = 0;
                            } 
                            res = handle_alias_parm(buffer, TRUE, FALSE);
                            if (res != NO_ERR) {
                                if (++errorcnt == MAX_ALIAS_ERRORS) {
                                    log_error("\nError: skipping aliases; "
                                              "too many errors\n");
                                    done = TRUE;
                                }
                            }
                        }  // else skip a newline or comment line
                    } // else skip empty line
                } else {
                    done = TRUE;
                }
            }
            fclose(fp);
            /* Save mtime of this aliases file */
            res = update_def_yangcli_file_mtime (ALIASES_FILE, fullspec);
        } else if (file_error) {
            log_error("\nError: Aliases file '%s' could not be opened\n",
                      fullspec);
            res = ERR_FIL_OPEN;
        } else if (LOGDEBUG) {
            log_debug("\nAliases file '%s' could not be opened\n", fullspec);
        }
    } else {
        log_error("\nError: Expand source '%s' failed (%s)",
                  fspec, get_error_string(res));
    }

    if (fullspec) {
        m__free(fullspec);
    }

    if (buffer) {
        m__free(buffer);
    }
    return res;

}  /* load_aliases */
コード例 #4
0
ファイル: agt_cli.c プロジェクト: fredericoschardong/escape
/********************************************************************
* FUNCTION agt_cli_process_input
*
* Process the param line parameters against the hardwired
* parmset for the netconfd program
*
* INPUTS:
*    argc == argument count
*    argv == array of command line argument strings
*    agt_profile == agent profile struct to fill in
*    showver == address of version return quick-exit status
*    showhelpmode == address of help return quick-exit status
*
* OUTPUTS:
*    *agt_profile is filled in, with parms gathered or defaults
*    *showver == TRUE if user requsted version quick-exit mode
*    *showhelpmode == requested help mode 
*                     (none, breief, normal, full)
*
* RETURNS:
*    NO_ERR if all goes well
*********************************************************************/
status_t
    agt_cli_process_input (int argc,
                           char *argv[],
                           agt_profile_t *agt_profile,
                           boolean *showver,
                           help_mode_t *showhelpmode)
{
    ncx_module_t          *mod;
    obj_template_t        *obj;
    val_value_t           *valset, *val;
    FILE                  *fp;
    status_t               res;
    boolean                test;

#ifdef DEBUG
    if (!argv || !agt_profile || !showver || !showhelpmode) {
        return SET_ERROR(ERR_INTERNAL_PTR);
    }
#endif

    *showver = FALSE;
    *showhelpmode = HELP_MODE_NONE;

    /* find the parmset definition in the registry */
    obj = NULL;
    mod = ncx_find_module(AGT_CLI_MODULE, NULL);
    if (mod) {
        obj = ncx_find_object(mod, AGT_CLI_CONTAINER);
    }
    if (!obj) {
        log_error("\nError: netconfd module with CLI definitions not loaded");
        return ERR_NCX_NOT_FOUND;
    }

    /* parse the command line against the object template */
    res = NO_ERR;
    valset = NULL;
    if (argc > 0) {
        valset = cli_parse(NULL,
                           argc, 
                           argv, 
                           obj,
                           FULLTEST, 
                           PLAINMODE, 
                           TRUE, 
                           CLI_MODE_PROGRAM,
                           &res);
        if (res != NO_ERR) {
            if (valset) {
                val_free_value(valset);
            }
            return res;
        }
    }

    if (valset != NULL) {
        /* transfer the parmset values */
        set_server_profile(valset, agt_profile);

        /* next get any params from the conf file */
        val = val_find_child(valset, 
                             AGT_CLI_MODULE, 
                             NCX_EL_CONFIG);
        if (val) {
            if (val->res == NO_ERR) {
                /* try the specified config location */
                agt_profile->agt_conffile = VAL_STR(val);
                res = conf_parse_val_from_filespec(VAL_STR(val), 
                                                   valset, 
                                                   TRUE, 
                                                   TRUE);
                if (res != NO_ERR) {
                    val_free_value(valset);
                    return res;
                } else {
                    /* transfer the parmset values again */
                    set_server_profile(valset, agt_profile);
                }
            }
        } else {
            fp = fopen((const char *)AGT_DEF_CONF_FILE, "r");
            if (fp != NULL) {
                fclose(fp);

                /* use default config location */
                res = conf_parse_val_from_filespec(AGT_DEF_CONF_FILE, 
                                                   valset, 
                                                   TRUE, 
                                                   TRUE);
                if (res != NO_ERR) {
                    val_free_value(valset);
                    return res;
                } else {
                    /* transfer the parmset values again */
                    set_server_profile(valset, agt_profile);
                }
            }
        }

        /* set the logging control parameters */
        val_set_logging_parms(valset);

        /* audit-log-append param */
        val = val_find_child(valset, 
                             AGT_CLI_MODULE, 
                             NCX_EL_AUDIT_LOG_APPEND);
        if (val && val->res == NO_ERR) {
            test = TRUE;
        } else {
            test = FALSE;
        }

        /* audit-log param */
        val = val_find_child(valset, AGT_CLI_MODULE, NCX_EL_AUDIT_LOG);
        if (val && val->res == NO_ERR) {
            xmlChar *filespec = ncx_get_source(VAL_STR(val), &res);
            if (filespec == NULL) {
                log_error("\nError: get source for audit log failed");
                return res;
            }
            res = log_audit_open((const char *)filespec, test, TRUE);
            if (res == NO_ERR) {
                if (LOGDEBUG) {
                    log_debug("\nAudit log '%s' opened for %s",
                              filespec,
                              (test) ? "append" : "write");
                }
            } else {
                log_error("\nError: open audit log '%s' failed",
                          filespec);
            }
            m__free(filespec);
            if (res != NO_ERR) {
                return res;
            }
        }

        /* set the file search path parms */
        res = val_set_path_parms(valset);
        if (res != NO_ERR) {
            return res;
        }

        /* set the warning control parameters */
        res = val_set_warning_parms(valset);
        if (res != NO_ERR) {
            return res;
        }

        /* set the feature code generation parameters */
        res = val_set_feature_parms(valset);
        if (res != NO_ERR) {
            return res;
        }

        /* check the subdirs parameter */
        res = val_set_subdirs_parm(valset);
        if (res != NO_ERR) {
            return res;
        }

        /* check the protocols parameter */
        res = val_set_protocols_parm(valset);
        if (res != NO_ERR) {
            return res;
        }

        /* check the system-sorted param */
        val = val_find_child(valset, AGT_CLI_MODULE, NCX_EL_SYSTEM_SORTED);
        if (val && val->res == NO_ERR) {
            agt_profile->agt_system_sorted = VAL_BOOL(val);
        }

        /* version param handled externally */

        /* check if version mode requested */
        val = val_find_child(valset, AGT_CLI_MODULE, NCX_EL_VERSION);
        *showver = (val) ? TRUE : FALSE;

        /* check if help mode requested */
        val = val_find_child(valset, AGT_CLI_MODULE, NCX_EL_HELP);
        if (val) {
            *showhelpmode = HELP_MODE_NORMAL;

            /* help submode parameter (brief/normal/full) */
            val = val_find_child(valset, AGT_CLI_MODULE, NCX_EL_BRIEF);
            if (val) {
                *showhelpmode = HELP_MODE_BRIEF;
            } else {
                /* full parameter */
                val = val_find_child(valset, AGT_CLI_MODULE, NCX_EL_FULL);
                if (val) {
                    *showhelpmode = HELP_MODE_FULL;
                }
            }
        }
    }

    /* cleanup and exit
     * handoff the malloced 'valset' memory here 
     */
    cli_val = valset;

    return res;

} /* agt_cli_process_input */