Skip to content

Commit

Permalink
add nvram_dump & fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankZhou committed Jun 19, 2013
1 parent ed3106a commit b7470b4
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 52 deletions.
4 changes: 2 additions & 2 deletions HOWTO
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ After doing this you can use attribute related functions as followings:

@(none):/tmp# nvram show lan_main_rule 0 name
LAN1
@(none):/tmp# nvram replace attribute lan_main_rule 0 name HELLO_WORLD
@(none):/tmp# nvram show lan_main_rule 0 name HELLO_WORLD
@(none):/tmp# nvram replace attr lan_main_rule 0 name HELLO_WORLD
@(none):/tmp# nvram show lan_main_rule 0 name
HELLO_WORLD
@(none):/tmp# nvram get lan_main_rule
HELLO_WORLD^1^1500^1^1^0|GuestLAN^0^1500^1^1^0
Expand Down
10 changes: 9 additions & 1 deletion cli.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <sys/types.h>
#include <signal.h>
#include "cli.h"
#include "nvram_rule.h"

Expand Down Expand Up @@ -331,7 +333,6 @@ int main( int argc, const char *argv[] )
int stat = 1;
int done = 0;
char res[NVRAM_BUF_LEN];
int i;

if( argc > 1 )
{
Expand Down Expand Up @@ -620,6 +621,12 @@ int main( int argc, const char *argv[] )
stat = nvram_commit();
done++;
}
/* nvram commit */
else if( !strncmp(*argv, "dump", 4) )
{
stat = nvram_dump();
done++;
}
else
{
fprintf(stderr, "Unknown option '%s' !\n", *argv);
Expand Down Expand Up @@ -654,6 +661,7 @@ int main( int argc, const char *argv[] )
" nvram factory\n"

" nvram commit\n"
" nvram dump\n"
);
}
return stat;
Expand Down
3 changes: 3 additions & 0 deletions nvram.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,8 @@ int nvram_import(char *filename);
//TODO
int nvram_upgrade(char *source);
int nvram_downgrade(char *target);

//helper function
int nvram_dump(void);
/********************************************************/
#endif /* _nvram_h_ */
1 change: 0 additions & 1 deletion nvram_fw.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#if defined(NVRAM_MMAP) && NVRAM_MMAP > 0
static char *nvram_buf = NULL;
#endif
static int nvram_fd = -1;
int check_action(void);
int file_to_buf(char *path, char *buf, int len);

Expand Down
13 changes: 13 additions & 0 deletions nvram_public.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "nvram.h"
#include "nvram_fw.h"
#include "nvram_factory.h"

/* Global */
nvram_handle_t *nvram_h = NULL;
extern struct nvram_fw_tuple nvram_fw_table[];
Expand Down Expand Up @@ -331,3 +332,15 @@ int nvram_downgrade(char *target)
return change;
}

int nvram_dump(void)
{
int ret = -1;
nvram_tuple_t * t = nvram_getall();
while (NULL != t ){
ret = 0;
printf("nvram set \"%s=%s\"\n", t->name, t->value);
t = t->next;
}
return ret;
}

49 changes: 20 additions & 29 deletions nvram_rule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "nvram.h"
#include "nvram_rule.h"

char *mask[] = {
Expand All @@ -16,7 +17,7 @@ char *mask[] = {
};


int show_enable_value(char *type, char *val, char *buf, int bsize)
int show_enable_value(char *val, char *buf, int bsize)
{
if (!val) {
return NVRAM_NO_ATTRIBUTE;
Expand All @@ -30,7 +31,7 @@ int show_enable_value(char *type, char *val, char *buf, int bsize)
return strlen(buf);
}

int show_value(char *type, char *val, char *buf, int bsize)
int show_value(char *val, char *buf, int bsize)
{
if (!val) {
return NVRAM_NO_ATTRIBUTE;
Expand All @@ -44,7 +45,7 @@ int show_value(char *type, char *val, char *buf, int bsize)

char *iftype[] = { "wan", "lan", NULL };

int show_if_value(char *type, char *val, char *buf, int bsize)
int show_if_value(char *val, char *buf, int bsize)
{
char *ptr;
int i, len, idx;
Expand Down Expand Up @@ -76,7 +77,7 @@ int show_if_value(char *type, char *val, char *buf, int bsize)
return strlen(buf);
}

int show_proto_value(char *type, char *val, char *buf, int bsize)
int show_proto_value(char *val, char *buf, int bsize)
{
if (!val) {
return NVRAM_NO_ATTRIBUTE;
Expand All @@ -102,7 +103,7 @@ int show_proto_value(char *type, char *val, char *buf, int bsize)
return strlen(buf);
}

int show_mask_value(char *type, char *val, char *buf, int bsize)
int show_mask_value(char *val, char *buf, int bsize)
{
if (!val) {
return NVRAM_NO_ATTRIBUTE;
Expand Down Expand Up @@ -1955,11 +1956,10 @@ int sep_string(char *word, const char *delim, char **idx_arr, int max_tok)
* \param[out] buf: a pointer to a buffer for copying the parsed data.
* \param[in] bsize: to specify the size of the imported area.
*/
int nvram_get_rule(char *rule_set, int nth,
int nvram_get_rule(const char *rule_set, int nth,
char *buf, int bsize)
{
char tmp[NVRAM_BUF_LEN];
char *next;
char *wordlist;
char *str, *ptr;
int ret;
Expand Down Expand Up @@ -2031,7 +2031,7 @@ int nvram_get_rule(char *rule_set, int nth,
* \param[out] buf: a pointer to a buffer for copying the parsed data.
* \param[in] bsize: to specify the size of the imported area.
*/
int nvram_get_subrule(char *rule_set, int nth,
int nvram_get_subrule(const char *rule_set, int nth,
int start, int end, char *buf, int bsize)
{
char *ptr_array[MAX_ATTR_NUM];
Expand Down Expand Up @@ -2088,8 +2088,8 @@ int nvram_get_subrule(char *rule_set, int nth,
* \param[out] buf: a pointer to a buffer for copying the parsed data.
* \param[in] bsize: to specify the size of the imported area.
*/
int nvram_get_attr_val(char *rule_set, int nth,
char *type, char *buf, int bsize, int use)
int nvram_get_attr_val(const char *rule_set, int nth,
const char *type, char *buf, int bsize, int use)
{
char *ptr_array[MAX_ATTR_NUM];
char word[NVRAM_BUF_LEN];
Expand Down Expand Up @@ -2124,13 +2124,13 @@ int nvram_get_attr_val(char *rule_set, int nth,
if (strcmp(attr[j].name, type) == 0) {
if (use == NVRAM_USE_CLI && attr[j].func_orig) {
/* Don't wrap. Just get the value stored in nvram. */
return attr[j].func_orig(type, ptr_array[j],
return attr[j].func_orig( ptr_array[j],
buf, bsize);
} else if (use == NVRAM_USE_WEB && attr[j].func_wrap) {
/* Wrap the value from nvram. Eg., the attribute
* <enable> is 1 will be wrapped to 'checked'.
*/
return attr[j].func_wrap(type, ptr_array[j], buf,
return attr[j].func_wrap( ptr_array[j], buf,
bsize);
}
}
Expand All @@ -2140,17 +2140,10 @@ int nvram_get_attr_val(char *rule_set, int nth,
return NVRAM_NO_ATTRIBUTE;
}

enum opcode {
ADD_RULE = 0,
DELETE_RULE = 1,
REPLACE_RULE = 2
};

int nvram_op_rule(char *rule_set, enum opcode op, int nth, char *new_rule)
int nvram_op_rule(const char *rule_set, enum opcode op, int nth, char *new_rule)
{
char buf[NVRAM_BUF_LEN];
char word[NVRAM_BUF_LEN];
char *wordlist;
char *ptr;
int len, ret, i, rule_num;
char *rule_sep = RULE_SEP;
Expand All @@ -2164,7 +2157,6 @@ int nvram_op_rule(char *rule_set, enum opcode op, int nth, char *new_rule)
len = NVRAM_BUF_LEN;
ptr = buf;
for (i = 0; i < rule_num; i++) {
char *rule;
ret = nvram_get_rule(rule_set, i, word, NVRAM_BUF_LEN);
if (ret < 0) {
return ret;
Expand Down Expand Up @@ -2233,7 +2225,7 @@ int nvram_op_rule(char *rule_set, enum opcode op, int nth, char *new_rule)
* \param[in] nth: to specify the nth rule in the rule set.
* \param[in] new_rule: the replacing rule.
*/
int nvram_replace_rule(char *rule_set, int nth, char *new_rule)
int nvram_replace_rule(const char *rule_set, int nth, char *new_rule)
{
int rule_num;

Expand Down Expand Up @@ -2262,7 +2254,7 @@ int nvram_replace_rule(char *rule_set, int nth, char *new_rule)
* \param[in] nth: to specify the nth rule in the rule set.
* \param[in] new_rule: the replacing rule.
*/
int nvram_replace_attr(char *rule_set, int nth,
int nvram_replace_attr(const char *rule_set, int nth,
char *attr_name, char *new_attr)
{
char *ptr_array[MAX_ATTR_NUM];
Expand Down Expand Up @@ -2329,7 +2321,7 @@ int nvram_replace_attr(char *rule_set, int nth,
* \param[in] rule_set: to specify the rule_set.
* \param[in] nth: to specify the nth rule in the rule set.
*/
int nvram_delete_rule(char *rule_set, int nth)
int nvram_delete_rule(const char *rule_set, int nth)
{
if (!rule_set || !*rule_set) {
return NVRAM_INVALID;
Expand All @@ -2347,7 +2339,7 @@ int nvram_delete_rule(char *rule_set, int nth)
* \param[in] nth: to specify the nth rule in the rule set.
* \param[in] new_rule: the replacing rule.
*/
int nvram_add_rule(char *rule_set, int nth, char *new_rule)
int nvram_add_rule(const char *rule_set, int nth, char *new_rule)
{
int rule_num;

Expand Down Expand Up @@ -2380,7 +2372,7 @@ int nvram_add_rule(char *rule_set, int nth, char *new_rule)
* \param[in] nth: to specify the nth rule in the rule set.
* \param[in] new_rule: the replacing rule.
*/
int nvram_prepend_rule(char *rule_set, char *new_rule)
int nvram_prepend_rule(const char *rule_set, char *new_rule)
{
if (!rule_set || !*rule_set || !new_rule) {
return NVRAM_INVALID;
Expand All @@ -2397,7 +2389,7 @@ int nvram_prepend_rule(char *rule_set, char *new_rule)
* \param[in] nth: to specify the nth rule in the rule set.
* \param[in] new_rule: the replacing rule.
*/
int nvram_append_rule(char *rule_set, char *new_rule)
int nvram_append_rule(const char *rule_set, char *new_rule)
{
char buf[NVRAM_BUF_LEN];
int len;
Expand Down Expand Up @@ -2433,9 +2425,8 @@ int nvram_append_rule(char *rule_set, char *new_rule)
* \return The number of subrule in the rule set.
* \param[in] rule_set: to specify the rule_set.
*/
int nvram_get_rule_num(char *rule_set)
int nvram_get_rule_num(const char *rule_set)
{
char *delim;
char *sep = RULE_SEP;
int i;
int count;
Expand Down
45 changes: 26 additions & 19 deletions nvram_rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,78 +18,85 @@

struct attr {
char *name;
int (*func_orig)(char *, char *, char *, int);
int (*func_wrap)(char *, char *, char *, int);
int (*func_orig)(char *, char *, int);
int (*func_wrap)(char *, char *, int);
};

enum opcode {
ADD_RULE = 0,
DELETE_RULE = 1,
REPLACE_RULE = 2
};

/******************************************/
int show_enable_value(char *type, char *val, char *buf, int bsize);
int show_value(char *type, char *val, char *buf, int bsize);
int show_if_value(char *type, char *val, char *buf, int bsize);
int show_proto_value(char *type, char *val, char *buf, int bsize);
int show_mask_value(char *type, char *val, char *buf, int bsize);
int show_enable_value(char *val, char *buf, int bsize);
int show_value(char *val, char *buf, int bsize);
int show_if_value(char *val, char *buf, int bsize);
int show_proto_value(char *val, char *buf, int bsize);
int show_mask_value(char *val, char *buf, int bsize);

int sep_string(char *word, const char *delim, char **idx_arr, int max_tok);

/**
* \brief Get a specified rule set from nvram, parse it by the RULE_SEP
* character, and return the nth rule.
*/
int nvram_get_rule(char *rule_set, int nth,
int nvram_get_rule(const char *rule_set, int nth,
char *buf, int bsize);

/**
* \brief Get a specified specified rule set from nvram, parse it by
* RULE_SEP character, and return the subrule with the attribute position in
* between start and end.
*/
int nvram_get_subrule(char *rule_set, int nth,
int nvram_get_subrule(const char *rule_set, int nth,
int start, int end, char *buf, int bsize);

/**
* \brief Get a specified attribute of the given rule from the rule set queried
* from nvram. The rules are separated by blank character and the attributes
* in a rule are separated by '-' character.
*/
int nvram_get_attr_val(char *rule_set, int nth,
char *type, char *buf, int bsize, int use);
int nvram_get_attr_val(const char *rule_set, int nth,
const char *type, char *buf, int bsize, int use);

//int nvram_op_rule(char *rule_set, enum opcode op, int nth, char *new_rule);
int nvram_op_rule(const char *rule_set, enum opcode op,
int nth, char *new_rule);

/**
* \brief Replace the nth rule in the given rule-name as the specific rule.
*/
int nvram_replace_rule(char *rule_set, int nth, char *new_rule);
int nvram_replace_rule(const char *rule_set, int nth, char *new_rule);

/**
* \brief Replace the mth attribute of the nth rule in the given rule-name as
* the specific rule.
*/
int nvram_replace_attr(char *rule_set, int nth,
int nvram_replace_attr(const char *rule_set, int nth,
char *attr, char *new_attr);

/**
* \brief Append a new rule into the given rule-name.
*/
int nvram_append_rule(char *rule_set, char *new_rule);
int nvram_append_rule(const char *rule_set, char *new_rule);

/**
* \brief Prepend a new rule into the given rule-name.
*/
int nvram_prepend_rule(char *rule_set, char *new_rule);
int nvram_prepend_rule(const char *rule_set, char *new_rule);

/**
* \brief Add a new rule in the given rule-name as the specific rule.
*/
int nvram_add_rule(char *rule_set, int nth, char *new_rule);
int nvram_add_rule(const char *rule_set, int nth, char *new_rule);

/**
* \brief Delete nth rule in the given rule-name.
*/
int nvram_delete_rule(char *rule_set, int nth);
int nvram_delete_rule(const char *rule_set, int nth);

/**
* \brief Get the number of rules in the given rule-name.
*/
int nvram_get_rule_num(char *rule_set);
int nvram_get_rule_num(const char *rule_set);
#endif

0 comments on commit b7470b4

Please sign in to comment.