📄 read_config.c
字号:
/* * read_config.c */#include <net-snmp/net-snmp-config.h>#include <stdio.h>#include <ctype.h>#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#include <sys/types.h>#if HAVE_SYS_PARAM_H#include <sys/param.h>#endif#if TIME_WITH_SYS_TIME# ifdef WIN32# include <sys/timeb.h># else# include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#ifdef HAVE_SYS_STAT_H#include <sys/stat.h>#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#if HAVE_SYS_SELECT_H#include <sys/select.h>#endif#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_SYS_SOCKET_H#include <sys/socket.h>#endif#if HAVE_NETDB_H#include <netdb.h>#endif#include <errno.h>#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include <net-snmp/types.h>#include <net-snmp/output_api.h>#include <net-snmp/config_api.h>#include <net-snmp/library/read_config.h> /* for "internal" definitions */#include <net-snmp/utilities.h>#include <net-snmp/library/mib.h>#include <net-snmp/library/parse.h>#include <net-snmp/library/snmp_api.h>#include <net-snmp/library/callback.h>
#include <windows.h>static int config_errors;struct config_files *config_files = NULL;struct config_line *register_prenetsnmp_mib_handler(const char *type, const char *token, void (*parser) (const char *, char *), void (*releaser) (void), const char *help){ struct config_line *ltmp; ltmp = register_config_handler(type, token, parser, releaser, help); if (ltmp != NULL) ltmp->config_time = PREMIB_CONFIG; return (ltmp);}struct config_line *register_app_prenetsnmp_mib_handler(const char *token, void (*parser) (const char *, char *), void (*releaser) (void), const char *help){ return (register_prenetsnmp_mib_handler (NULL, token, parser, releaser, help));}/*******************************************************************-o-****** * register_config_handler * * Parameters: * *type * *token * *parser * *releaser * * Returns: * Pointer to a new config line entry -OR- NULL on error. */struct config_line *register_config_handler(const char *type_param, const char *token, void (*parser) (const char *, char *), void (*releaser) (void), const char *help){ struct config_files **ctmp = &config_files; struct config_line **ltmp; const char *type = type_param; if (type == NULL) { type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPTYPE); } /* * Find type in current list -OR- create a new file type. */ while (*ctmp != NULL && strcmp((*ctmp)->fileHeader, type)) { ctmp = &((*ctmp)->next); } if (*ctmp == NULL) { *ctmp = (struct config_files *) calloc(1, sizeof(struct config_files)); if (!*ctmp) { return NULL; } (*ctmp)->fileHeader = strdup(type); } /* * Find parser type in current list -OR- create a new * line parser entry. */ ltmp = &((*ctmp)->start); while (*ltmp != NULL && strcmp((*ltmp)->config_token, token)) { ltmp = &((*ltmp)->next); } if (*ltmp == NULL) { *ltmp = (struct config_line *) calloc(1, sizeof(struct config_line)); if (!*ltmp) { return NULL; } (*ltmp)->config_time = NORMAL_CONFIG; (*ltmp)->config_token = strdup(token); if (help != NULL) (*ltmp)->help = strdup(help); } /* * Add/Replace the parse/free functions for the given line type * in the given file type. */ (*ltmp)->parse_line = parser; (*ltmp)->free_func = releaser; return (*ltmp);} /* end register_config_handler() */struct config_line *register_app_config_handler(const char *token, void (*parser) (const char *, char *), void (*releaser) (void), const char *help){ return (register_config_handler(NULL, token, parser, releaser, help));}voidunregister_config_handler(const char *type_param, const char *token){ struct config_files **ctmp = &config_files; struct config_line **ltmp, *ltmp2; const char *type = type_param; if (type == NULL) { type = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_APPTYPE); } /* * find type in current list */ while (*ctmp != NULL && strcmp((*ctmp)->fileHeader, type)) { ctmp = &((*ctmp)->next); } if (*ctmp == NULL) { /* * Not found, return. */ return; } ltmp = &((*ctmp)->start); if (*ltmp == NULL) { /* * Not found, return. */ return; } if (strcmp((*ltmp)->config_token, token) == 0) { /* * found it at the top of the list */ ltmp2 = (*ltmp)->next; free((*ltmp)->config_token); SNMP_FREE((*ltmp)->help); free(*ltmp); (*ctmp)->start = ltmp2; return; } while ((*ltmp)->next != NULL && strcmp((*ltmp)->next->config_token, token)) { ltmp = &((*ltmp)->next); } if ((*ltmp)->next != NULL) { free((*ltmp)->next->config_token); SNMP_FREE((*ltmp)->next->help); ltmp2 = (*ltmp)->next->next; free((*ltmp)->next); (*ltmp)->next = ltmp2; }}voidunregister_app_config_handler(const char *token){ unregister_config_handler(NULL, token);}voidunregister_all_config_handlers(){ struct config_files *ctmp, *save; struct config_line *ltmp; free_config(); /* * Keep using config_files until there are no more! */ for (ctmp = config_files; ctmp;) { for (ltmp = ctmp->start; ltmp; ltmp = ctmp->start) { unregister_config_handler(ctmp->fileHeader, ltmp->config_token); } free(ctmp->fileHeader); save = ctmp->next; free(ctmp); ctmp = save; config_files = save; }}#ifdef TESTINGvoidprint_config_handlers(void){ struct config_files *ctmp = config_files; struct config_line *ltmp; for (; ctmp != NULL; ctmp = ctmp->next) { DEBUGMSGTL(("read_config", "read_conf: %s\n", ctmp->fileHeader)); for (ltmp = ctmp->start; ltmp != NULL; ltmp = ltmp->next) DEBUGMSGTL(("read_config", " %s\n", ltmp->config_token)); }}#endifint linecount;const char *curfilename;struct config_line *read_config_get_handlers(const char *type){ struct config_files *ctmp = config_files; for (; ctmp != NULL && strcmp(ctmp->fileHeader, type); ctmp = ctmp->next); if (ctmp) return ctmp->start; return NULL;}voidread_config_with_type(const char *filename, const char *type){ struct config_line *ctmp = read_config_get_handlers(type); if (ctmp) read_config(filename, ctmp, EITHER_CONFIG); else DEBUGMSGTL(("read_config", "read_config: I have no registrations for type:%s,file:%s\n", type, filename));}struct config_line *read_config_find_handler(struct config_line *line_handlers, const char *token){ struct config_line *lptr; for (lptr = line_handlers; lptr != NULL; lptr = lptr->next) { if (!strcasecmp(token, lptr->config_token)) { return lptr; } } return NULL;}/* * searches a config_line linked list for a match */intrun_config_handler(struct config_line *lptr, const char *token, char *cptr, int when){ char tmpbuf[STRINGMAX]; lptr = read_config_find_handler(lptr, token); if (lptr != NULL) { if (when == EITHER_CONFIG || lptr->config_time == when) { DEBUGMSGTL(("read_config", "Found a parser. Calling it: %s / %s\n", token, cptr)); (*(lptr->parse_line)) (token, cptr); } } else if (when != PREMIB_CONFIG && !netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NO_TOKEN_WARNINGS)) { snprintf(tmpbuf, sizeof(tmpbuf), "Unknown token: %s.", token); tmpbuf[ sizeof(tmpbuf)-1 ] = 0; config_pwarn(tmpbuf); return SNMPERR_GENERR; } return SNMPERR_SUCCESS;}/* * takens an arbitrary string and tries to intepret it based on the * known configruation handlers for all registered types. May produce * inconsistent results when multiple tokens of the same name are * registered under different file types. *//* * we allow = delimeters here */#define SNMP_CONFIG_DELIMETERS " \t="intsnmp_config_when(char *line, int when){ char *cptr, buf[STRINGMAX], tmpbuf[STRINGMAX]; struct config_line *lptr = NULL; struct config_files *ctmp = config_files; if (line == NULL) { config_perror("snmp_config() called with a null string."); return SNMPERR_GENERR; } strncpy(buf, line, STRINGMAX); buf[STRINGMAX - 1] = '\0'; cptr = strtok(buf, SNMP_CONFIG_DELIMETERS); if (cptr && cptr[0] == '[') { if (cptr[strlen(cptr) - 1] != ']') { config_perror("no matching ']'"); return SNMPERR_GENERR; } lptr = read_config_get_handlers(cptr + 1); if (lptr == NULL) { snprintf(tmpbuf, sizeof(tmpbuf), "No handlers regestered for type %s.", cptr + 1); tmpbuf[ sizeof(tmpbuf)-1 ] = 0; config_perror(tmpbuf); return SNMPERR_GENERR; } cptr = strtok(NULL, SNMP_CONFIG_DELIMETERS); lptr = read_config_find_handler(lptr, cptr); } else { /* * we have to find a token */ for (; ctmp != NULL && lptr == NULL; ctmp = ctmp->next) lptr = read_config_find_handler(ctmp->start, cptr); } if (lptr == NULL && netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NO_TOKEN_WARNINGS)) { snprintf(tmpbuf, sizeof(tmpbuf), "Unknown token: %s.", cptr); tmpbuf[ sizeof(tmpbuf)-1 ] = 0; config_pwarn(tmpbuf); return SNMPERR_GENERR;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -