diff options
author | An-Cheng Huang <ancheng@vyatta.com> | 2010-07-28 19:08:43 -0700 |
---|---|---|
committer | An-Cheng Huang <ancheng@vyatta.com> | 2010-07-28 19:08:43 -0700 |
commit | a56723cbb6424e1d49289efceeb9c251eed324bb (patch) | |
tree | 79f97da29cc8e1aa972ec978703622a61a34ae5b /src | |
parent | de31f1befb8a92cecadd0c716f7313bd578511f9 (diff) | |
download | vyatta-cfg-a56723cbb6424e1d49289efceeb9c251eed324bb.tar.gz vyatta-cfg-a56723cbb6424e1d49289efceeb9c251eed324bb.zip |
add cstore-specific header file
Diffstat (limited to 'src')
-rw-r--r-- | src/cli_bin.cpp | 5 | ||||
-rw-r--r-- | src/cli_cstore.h | 147 | ||||
-rw-r--r-- | src/cli_new.c | 20 | ||||
-rw-r--r-- | src/cli_shell_api.cpp | 5 | ||||
-rw-r--r-- | src/cli_val.h | 110 | ||||
-rw-r--r-- | src/cstore/cstore-c.cpp | 11 | ||||
-rw-r--r-- | src/cstore/cstore-c.h | 7 | ||||
-rw-r--r-- | src/cstore/cstore-varref.cpp | 8 | ||||
-rw-r--r-- | src/cstore/cstore-varref.hpp | 2 | ||||
-rw-r--r-- | src/cstore/cstore.cpp | 37 | ||||
-rw-r--r-- | src/cstore/cstore.hpp | 8 | ||||
-rw-r--r-- | src/cstore/unionfs/cstore-unionfs.cpp | 19 | ||||
-rw-r--r-- | src/cstore/unionfs/cstore-unionfs.hpp | 13 |
13 files changed, 209 insertions, 183 deletions
diff --git a/src/cli_bin.cpp b/src/cli_bin.cpp index 420d19c..25a86ce 100644 --- a/src/cli_bin.cpp +++ b/src/cli_bin.cpp @@ -20,10 +20,7 @@ #include <string> #include <libgen.h> -extern "C" { -#include <cli_val.h> -} - +#include <cli_cstore.h> #include <cstore/unionfs/cstore-unionfs.hpp> static int op_idx = -1; diff --git a/src/cli_cstore.h b/src/cli_cstore.h new file mode 100644 index 0000000..c03bddf --- /dev/null +++ b/src/cli_cstore.h @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2010 Vyatta, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _CLI_VAL_CSTORE_H_ +#define _CLI_VAL_CSTORE_H_ +#ifdef __cplusplus +extern "C" { +#endif + +/* this header file contains all definitions/declarations in the original + * CLI implementation that are needed by the cstore library. + */ + +/* types */ +typedef int boolean; + +typedef enum { + ERROR_TYPE, + INT_TYPE, + IPV4_TYPE, + IPV4NET_TYPE, + IPV6_TYPE, + IPV6NET_TYPE, + MACADDR_TYPE, + DOMAIN_TYPE, /*end of addr types */ + TEXT_TYPE, + BOOL_TYPE, + PRIORITY_TYPE +} vtw_type_e; + +typedef struct { + vtw_type_e val_type; + char *val; + int cnt; /* >0 means multivalue */ + char **vals; /* We might union with val */ + vtw_type_e *val_types; /* used with vals and multitypes */ + boolean free_me; +} valstruct; + +typedef enum { + LIST_OP, /* right is next, left is list elem */ + HELP_OP, /* right is help string, left is elem */ + EXEC_OP, /* left command string, right help string */ + PATTERN_OP, /* left to var, right to pattern */ + OR_OP, + AND_OP, + NOT_OP, + COND_OP, /* aux field specifies cond type (GT, GE, etc.)*/ + VAL_OP, /* for strings used in other nodes */ + VAR_OP, /* string points to var */ + B_QUOTE_OP, /* string points to operand to be executed */ + ASSIGN_OP /* left to var, right to exp */ +} vtw_oper_e; + +typedef struct vtw_node{ + vtw_oper_e vtw_node_oper; + struct vtw_node *vtw_node_left; + struct vtw_node *vtw_node_right; + char *vtw_node_string; + int vtw_node_aux; + vtw_type_e vtw_node_type; + valstruct vtw_node_val; /* we'll union it later */ +} vtw_node; + +typedef struct { + vtw_node *vtw_list_head; + vtw_node *vtw_list_tail; +} vtw_list; + +typedef enum { + delete_act, + create_act, + activate_act, + update_act, + syntax_act, + commit_act, + begin_act, + end_act, + top_act +} vtw_act_type; + +typedef struct { + vtw_type_e def_type; + vtw_type_e def_type2; + char *def_type_help; + char *def_node_help; + char *def_default; + unsigned int def_priority; + char *def_priority_ext; + char *def_enumeration; + char *def_comp_help; + char *def_allowed; + char *def_val_help; + unsigned int def_tag; + unsigned int def_multi; + boolean tag; + boolean multi; + vtw_list actions[top_act]; + int is_value; /* this is used by the config store to indicate whether + * the last path component is a "value". */ +} vtw_def; + +/* extern variables */ +extern void *var_ref_handle; +extern FILE *out_stream; + +/* note that some functions may be used outside the actual CLI operations, + * so output may not have been initialized. nop in such cases. + */ +#define OUTPUT_USER(fmt, args...) do \ + { \ + if (out_stream) { \ + fprintf(out_stream, fmt , ##args); \ + } \ + } while (0); + +/* functions */ +const valstruct *get_syntax_self_in_valstruct(vtw_node *vnode); +int get_shell_command_output(const char *cmd, char *buf, + unsigned int buf_size); +int parse_def(vtw_def *defp, const char *path, boolean type_only); +boolean validate_value(vtw_def *def, char *value); +const char *type_to_name(vtw_type_e type); +int initialize_output(const char *op); +void bye(const char *msg, ...) __attribute__((format(printf, 1, 2), noreturn)); + +/* functions from cli_objects */ +char *get_at_string(void); + +#ifdef __cplusplus +} +#endif +#endif /* _CLI_VAL_CSTORE_H_ */ + diff --git a/src/cli_new.c b/src/cli_new.c index 9cc9777..a047f36 100644 --- a/src/cli_new.c +++ b/src/cli_new.c @@ -1471,18 +1471,19 @@ static int eval_va(valstruct *res, vtw_node *node) * cstore implementation. * handle is set => we are in cstore operation. */ - clind_val cv; - if (!cstore_get_var_ref(var_ref_handle, pathp, &cv, + vtw_type_e vtype; + char *vptr = NULL; + if (!cstore_get_var_ref(var_ref_handle, pathp, &vtype, &vptr, is_in_delete_action())) { status = -1; } else { /* success */ status = 0; - if(cv.value) { - res->val_type = cv.val_type; + if(vptr) { + res->val_type = vtype; res->val_types = NULL; res->free_me = TRUE; - res->val = cv.value; + res->val = vptr; } } } else { @@ -1648,10 +1649,11 @@ static int expand_string(char *stringp) * cstore implementation. * handle is set => we are in cstore operation. */ - clind_val cv; - if (cstore_get_var_ref(var_ref_handle, scanp, &cv, - is_in_delete_action())) { - cp=cv.value; + vtw_type_e vtype; + char *vptr = NULL; + if (cstore_get_var_ref(var_ref_handle, scanp, &vtype, &vptr, + is_in_delete_action()) && vptr) { + cp = vptr; } } else { /* legacy usage */ diff --git a/src/cli_shell_api.cpp b/src/cli_shell_api.cpp index 0962c80..22a5cad 100644 --- a/src/cli_shell_api.cpp +++ b/src/cli_shell_api.cpp @@ -22,10 +22,7 @@ #include <libgen.h> #include <sys/mount.h> -extern "C" { -#include <cli_val.h> -} - +#include <cli_cstore.h> #include <cstore/unionfs/cstore-unionfs.hpp> static int op_idx = -1; diff --git a/src/cli_val.h b/src/cli_val.h index 41e4461..536d964 100644 --- a/src/cli_val.h +++ b/src/cli_val.h @@ -2,9 +2,10 @@ #define CLI_DEF_H #include <stdio.h> +#include <cli_cstore.h> + #define BITWISE 0 /* no partial commit */ -#define boolean int #ifndef FALSE #define FALSE 0 #endif @@ -21,19 +22,6 @@ typedef enum { create_mode, update_mode }vtw_cmode; -typedef enum { - ERROR_TYPE, - INT_TYPE, - IPV4_TYPE, - IPV4NET_TYPE, - IPV6_TYPE, - IPV6NET_TYPE, - MACADDR_TYPE, - DOMAIN_TYPE, /*end of addr types */ - TEXT_TYPE, - BOOL_TYPE, - PRIORITY_TYPE -}vtw_type_e; typedef enum { EQ_COND = 1, @@ -47,83 +35,11 @@ typedef enum { }vtw_cond_e; /* IN_COND is like EQ for singular compare, but OR for multivalue right operand */ -typedef enum { - LIST_OP, /* right is next, left is list elem */ - HELP_OP, /* right is help string, left is elem */ - EXEC_OP, /* left command string, right help string */ - PATTERN_OP, /* left to var, right to pattern */ - OR_OP, - AND_OP, - NOT_OP, - COND_OP, /* aux field specifies cond type (GT, GE, etc.)*/ - VAL_OP, /* for strings used in other nodes */ - VAR_OP, /* string points to var */ - B_QUOTE_OP, /* string points to operand to be executed */ - ASSIGN_OP /* left to var, right to exp */ -}vtw_oper_e; - -typedef struct { - vtw_type_e val_type; - char *val; - int cnt; /* >0 means multivalue */ - char **vals; /* We might union with val */ - vtw_type_e *val_types; /* used with vals and multitypes */ - boolean free_me; -}valstruct; - -typedef struct vtw_node{ - vtw_oper_e vtw_node_oper; - struct vtw_node *vtw_node_left; - struct vtw_node *vtw_node_right; - char *vtw_node_string; - int vtw_node_aux; - vtw_type_e vtw_node_type; - valstruct vtw_node_val; /* we'll union it later */ -}vtw_node; - -typedef struct { - vtw_node *vtw_list_head; - vtw_node *vtw_list_tail; -}vtw_list; - typedef struct { int t_lev; int m_lev; }vtw_mark; -typedef enum { - delete_act, - create_act, - activate_act, - update_act, - syntax_act, - commit_act, - begin_act, - end_act, - top_act -}vtw_act_type; - -typedef struct { - vtw_type_e def_type; - vtw_type_e def_type2; - char *def_type_help; - char *def_node_help; - char *def_default; - unsigned int def_priority; - char *def_priority_ext; - char *def_enumeration; - char *def_comp_help; - char *def_allowed; - char *def_val_help; - unsigned int def_tag; - unsigned int def_multi; - boolean tag; - boolean multi; - vtw_list actions[top_act]; - int is_value; /* this is used by the config store to indicate whether - * the last path component is a "value". */ -}vtw_def; - typedef struct { const char *f_segp; int f_seglen; @@ -162,10 +78,6 @@ extern vtw_node *make_str_node(char *str); extern vtw_node *make_var_node(char *str); extern vtw_node *make_str_node0(char *str, vtw_oper_e op); extern void append(vtw_list *l, vtw_node *n, int aux); -const valstruct *get_syntax_self_in_valstruct(vtw_node *vnode); -int get_shell_command_output(const char *cmd, char *buf, - unsigned int buf_size); -extern int parse_def(vtw_def *defp, const char *path, boolean type_only); extern int yy_cli_val_lex(void); extern void cli_val_start(char *s); @@ -178,7 +90,6 @@ extern void free_def(vtw_def *defp); extern void free_sorted(vtw_sorted *sortp); extern vtw_path m_path, t_path; -extern void *var_ref_handle; /************************************************* GLOBAL FUNCTIONS @@ -193,14 +104,10 @@ extern boolean val_cmp(const valstruct *left, const valstruct *right, vtw_cond_e cond); extern void out_of_memory(void) __attribute__((noreturn)); extern void subtract_values(char **lhs, const char *rhs); -extern boolean validate_value(vtw_def *def, - char *value); extern void internal_error(int line, const char *file) __attribute__((noreturn)); extern void done(void); extern void del_value(vtw_def *defp, char *cp); -extern void bye(const char *msg, ...) - __attribute__((format(printf, 1, 2), noreturn)); extern void print_msg(const char *msg, ...) __attribute__((format(printf, 1, 2))); extern void switch_path(first_seg *seg); @@ -209,7 +116,6 @@ extern void free_val(valstruct *val); extern void touch(void); extern int mkdir_p(const char *path); -extern const char *type_to_name(vtw_type_e type); extern boolean execute_list(vtw_node *cur, vtw_def *def, const char **outbuf); extern void touch_dir(const char *dp); extern void touch_file(const char *name); @@ -237,20 +143,8 @@ extern int get_config_lock(void); #define LOGFILE_STDERR "/tmp/cfg-stderr.log" extern int out_fd; -extern FILE *out_stream; extern FILE *err_stream; -extern int initialize_output(const char *op); -/* note that some functions may be used outside the actual CLI operations, - * so output may not have been initialized. nop in such cases. - */ -#define OUTPUT_USER(fmt, args...) do \ - { \ - if (out_stream) { \ - fprintf(out_stream, fmt , ##args); \ - } \ - } while (0); - /* debug hooks? */ #define my_malloc(size, name) malloc(size) #define my_realloc(ptr, size, name) realloc(ptr, size) diff --git a/src/cstore/cstore-c.cpp b/src/cstore/cstore-c.cpp index 3215707..0e25b3c 100644 --- a/src/cstore/cstore-c.cpp +++ b/src/cstore/cstore-c.cpp @@ -18,8 +18,8 @@ #include <vector> #include <string> -#include "cstore-c.h" -#include "cstore/unionfs/cstore-unionfs.hpp" +#include <cstore/cstore-c.h> +#include <cstore/unionfs/cstore-unionfs.hpp> void * cstore_init(void) @@ -80,12 +80,13 @@ cstore_cfg_path_exists(void *handle, const char *path_comps[], int num_comps) } int -cstore_get_var_ref(void *handle, const char *ref_str, clind_val *cval, - int from_active) +cstore_get_var_ref(void *handle, const char *ref_str, vtw_type_e *type, + char **val, int from_active) { if (handle) { Cstore *cs = (Cstore *) handle; - return (cs->getVarRef(ref_str, *cval, from_active) ? 1 : 0); + *val = cs->getVarRef(ref_str, *type, from_active); + return (*val ? 1 : 0); } return 0; } diff --git a/src/cstore/cstore-c.h b/src/cstore/cstore-c.h index e664f95..c2c5fa0 100644 --- a/src/cstore/cstore-c.h +++ b/src/cstore/cstore-c.h @@ -20,8 +20,7 @@ extern "C" { #endif -#include <cli_val.h> -#include <cli_val_engine.h> +#include <cli_cstore.h> void *cstore_init(void); void cstore_free(void *handle); @@ -39,8 +38,8 @@ int cstore_cfg_path_deactivated(void *handle, const char *path_comps[], * during cstore operations since they operate on "current" paths constructed * by the operations. */ -int cstore_get_var_ref(void *handle, const char *ref_str, clind_val *cval, - int from_active); +int cstore_get_var_ref(void *handle, const char *ref_str, vtw_type_e *type, + char **val, int from_active); int cstore_set_var_ref(void *handle, const char *ref_str, const char *value, int to_active); diff --git a/src/cstore/cstore-varref.cpp b/src/cstore/cstore-varref.cpp index 6d71307..46950fa 100644 --- a/src/cstore/cstore-varref.cpp +++ b/src/cstore/cstore-varref.cpp @@ -19,12 +19,8 @@ #include <string> #include <algorithm> -#include "cstore-varref.hpp" - -extern "C" { -#include "cli_val.h" -#include "cli_objects.h" -} +#include <cli_cstore.h> +#include <cstore/cstore-varref.hpp> using namespace std; diff --git a/src/cstore/cstore-varref.hpp b/src/cstore/cstore-varref.hpp index 1fc1d52..7ab523f 100644 --- a/src/cstore/cstore-varref.hpp +++ b/src/cstore/cstore-varref.hpp @@ -20,7 +20,7 @@ #include <string> #include <map> -#include "cstore.hpp" +#include <cstore/cstore.hpp> using namespace std; diff --git a/src/cstore/cstore.cpp b/src/cstore/cstore.cpp index 31c896a..e97ea2c 100644 --- a/src/cstore/cstore.cpp +++ b/src/cstore/cstore.cpp @@ -25,12 +25,9 @@ #include <algorithm> #include <sstream> -extern "C" { -#include "cli_val.h" -} - -#include "cstore.hpp" -#include "cstore-varref.hpp" +#include <cli_cstore.h> +#include <cstore/cstore.hpp> +#include <cstore/cstore-varref.hpp> ////// constants @@ -57,8 +54,9 @@ const string Cstore::C_ENV_SHAPI_COMP_HELP = "_cli_shell_api_comp_help"; const string Cstore::C_ENV_SHAPI_HELP_ITEMS = "_cli_shell_api_hitems"; const string Cstore::C_ENV_SHAPI_HELP_STRS = "_cli_shell_api_hstrs"; -//// dirs +//// dirs/files const string Cstore::C_ENUM_SCRIPT_DIR = "/opt/vyatta/share/enumeration"; +const string Cstore::C_LOGFILE_STDOUT = "/tmp/cfg-stdout.log"; ////// constructors/destructors /* this constructor just returns the generic environment string, @@ -1691,25 +1689,24 @@ Cstore::cfgPathGetEffectiveValues(const vector<string>& path_comps, /* get the value string that corresponds to specified variable ref string. * ref_str: var ref string (e.g., "./cost/@"). - * cval: (output) contains the resulting string. + * type: (output) the node type. * from_active: if true, value string should come from "active config". * otherwise from "working config". - * return true if successful. otherwise return false. + * return a pointer to the value string if successful (caller must free). + * otherwise return NULL. */ -bool -Cstore::getVarRef(const string& ref_str, clind_val& cval, bool from_active) +char * +Cstore::getVarRef(const string& ref_str, vtw_type_e& type, bool from_active) { - bool ret = true; + char *ret = NULL; SAVE_PATHS; VarRef vref(this, ref_str, from_active); string val; - vtw_type_e type; - if (!vref.getValue(val, type)) { - ret = false; - } else { - cval.val_type = type; + vtw_type_e t; + if (vref.getValue(val, t)) { + type = t; // follow original implementation. caller is supposed to free this. - cval.value = strdup(val.c_str()); + ret = strdup(val.c_str()); } RESTORE_PATHS; return ret; @@ -1855,8 +1852,8 @@ Cstore::output_internal(const char *fmt, ...) int fdout = -1; FILE *fout = NULL; do { - // XXX for now use the constant from cli_val.h - if ((fdout = open(LOGFILE_STDOUT, O_WRONLY | O_CREAT, 0660)) == -1) { + if ((fdout = open(C_LOGFILE_STDOUT.c_str(), + O_WRONLY | O_CREAT, 0660)) == -1) { break; } if (lseek(fdout, 0, SEEK_END) == ((off_t) -1)) { diff --git a/src/cstore/cstore.hpp b/src/cstore/cstore.hpp index f6a4215..1d4ffe2 100644 --- a/src/cstore/cstore.hpp +++ b/src/cstore/cstore.hpp @@ -20,10 +20,7 @@ #include <string> #include <map> -extern "C" { -#include <cli_val.h> -#include <cli_val_engine.h> -} +#include <cli_cstore.h> #define exit_internal(fmt, args...) do \ { \ @@ -65,6 +62,7 @@ public: static const string C_ENV_SHAPI_HELP_STRS; static const string C_ENUM_SCRIPT_DIR; + static const string C_LOGFILE_STDOUT; static const size_t MAX_CMD_OUTPUT_SIZE = 4096; @@ -245,7 +243,7 @@ public: * the limitations of the original CLI library implementation and MUST NOT * be used by anyone other than the original CLI library. */ - bool getVarRef(const string& ref_str, clind_val& cval, bool from_active); + char *getVarRef(const string& ref_str, vtw_type_e& type, bool from_active); bool setVarRef(const string& ref_str, const string& value, bool to_active); protected: diff --git a/src/cstore/unionfs/cstore-unionfs.cpp b/src/cstore/unionfs/cstore-unionfs.cpp index 977a597..6e6e5de 100644 --- a/src/cstore/unionfs/cstore-unionfs.cpp +++ b/src/cstore/unionfs/cstore-unionfs.cpp @@ -24,12 +24,8 @@ #include <errno.h> #include <sys/mount.h> -extern "C" { -#include "cli_val.h" -#include "cli_objects.h" -} - -#include "cstore-unionfs.hpp" +#include <cli_cstore.h> +#include <cstore/unionfs/cstore-unionfs.hpp> ////// constants @@ -61,6 +57,9 @@ const string UnionfsCstore::C_MARKER_CHANGED = ".modified"; const string UnionfsCstore::C_MARKER_UNSAVED = ".unsaved"; const string UnionfsCstore::C_COMMITTED_MARKER_FILE = "/tmp/.changes"; const string UnionfsCstore::C_COMMENT_FILE = ".comment"; +const string UnionfsCstore::C_TAG_NAME = "node.tag"; +const string UnionfsCstore::C_VAL_NAME = "node.val"; +const string UnionfsCstore::C_DEF_NAME = "node.def"; ////// static @@ -414,9 +413,9 @@ UnionfsCstore::tmpl_node_exists() bool UnionfsCstore::tmpl_parse(vtw_def& def) { - push_tmpl_path(DEF_NAME); + push_tmpl_path(C_DEF_NAME); bool ret = (b_fs::exists(tmpl_path) && b_fs::is_regular(tmpl_path) - && parse_def(&def, tmpl_path.file_string().c_str(), FALSE) == 0); + && parse_def(&def, tmpl_path.file_string().c_str(), 0) == 0); pop_tmpl_path(); return ret; } @@ -490,8 +489,8 @@ UnionfsCstore::get_all_child_node_names_impl(vector<string>& cnodes, * node.val * def */ - if (b_fs::exists(p / VAL_NAME) && b_fs::is_regular(p / VAL_NAME)) { - cnodes.push_back(VAL_NAME); + if (b_fs::exists(p / C_VAL_NAME) && b_fs::is_regular(p / C_VAL_NAME)) { + cnodes.push_back(C_VAL_NAME); } if (b_fs::exists(p / C_MARKER_DEF_VALUE) && b_fs::is_regular(p / C_MARKER_DEF_VALUE)) { diff --git a/src/cstore/unionfs/cstore-unionfs.hpp b/src/cstore/unionfs/cstore-unionfs.hpp index 357e307..dd44d9a 100644 --- a/src/cstore/unionfs/cstore-unionfs.hpp +++ b/src/cstore/unionfs/cstore-unionfs.hpp @@ -26,13 +26,9 @@ #include <boost/filesystem.hpp> +#include <cli_cstore.h> #include <cstore/cstore.hpp> -extern "C" { -#include <cli_val.h> -#include <cli_val_engine.h> -} - namespace b_fs = boost::filesystem; class UnionfsCstore : public Cstore { @@ -71,6 +67,9 @@ private: static const string C_MARKER_UNSAVED; static const string C_COMMITTED_MARKER_FILE; static const string C_COMMENT_FILE; + static const string C_TAG_NAME; + static const string C_VAL_NAME; + static const string C_DEF_NAME; static const size_t MAX_FILE_READ_SIZE = 8192; @@ -93,7 +92,7 @@ private: push_path(tmpl_path, new_comp); }; void push_tmpl_path_tag() { - push_tmpl_path(TAG_NAME); + push_tmpl_path(C_TAG_NAME); }; string pop_tmpl_path() { return pop_path(tmpl_path); @@ -102,7 +101,7 @@ private: push_path(mutable_cfg_path, new_comp); }; void push_cfg_path_val() { - push_cfg_path(VAL_NAME); + push_cfg_path(C_VAL_NAME); }; string pop_cfg_path() { return pop_path(mutable_cfg_path); |