nvparms.c

来自「在ARM7和UC/OSII的平台上实现了GPS自动报站的功能,涉及GPS模块LE」· C语言 代码 · 共 1,211 行 · 第 1/3 页

C
1,211
字号
   nv_fprintf(fp, comport_nvformats[i++].pattern, comport_nvparms.comport);

   nv_fprintf(fp, comport_nvformats[i++].pattern, 
              (comport_nvparms.LineProtocol == PPP) ? "PPP":"SLIP");

   return 0;
}
#endif   /* USE_COMPORT */

/* FUNCTION: nv_bool()
 *
 * set a passed boolean variable from a passed string
 * 
 * PARAM1: char * string
 * PARAM2: int * boolptr
 *
 * RETURNS: 
 */

void
nv_bool(char * string, int * boolptr)
{
   if (stricmp(string, "yes") == 0)
      *boolptr = TRUE;
   else if(stricmp(string, "true") == 0)
      *boolptr = TRUE;
   else
      *boolptr = FALSE;
}

/* FUNCTION: edit_nv_params()
 * Edit the value of the param in the nvram structure.
 * This function allows any (well almost) nvram parameter to be
 * configured from the command prompt. 
 * Usage:
 *    nvedit <whole line as it appears in webport.nv file>
 *    nvedit <str>: <value>
 * Example:
 *    nvedit PPP Console Logging: YES
 * 
 * PARAM1: void * pio
 *
 * RETURNS: 
 */

extern   char *   nextarg(char*); /* get next arg from a string */

int
edit_nv_params(void * pio)
{
   char *retstr=NULL;
   char *   cp;

   /* see if user put name on cmd line */
   cp = nextarg(((GEN_IO)pio)->inbuf);
   if (!cp || !*cp)
   {
      ns_printf(pio, "usage: nvedit <name of variable>: <value>\n");
      return -1;
   }

   retstr = get_nv_value(cp, nv_formats);
   if (retstr)
   {
      ns_printf(pio, retstr);
      return -1;
   }

   return 0;
}

/* FUNCTION: install_nvformat()
 *
 * install_nvformat() - Link a new nvparm_format structure to the end 
 * of a list of nvparm_format structures.
 *
 * PARAM1: struct nvparm_format * new_nvformat
 * PARAM2: struct nvparm_format * head_nvformat
 *
 * RETURNS: 0 for OK, -1 for Error.
 */

int
install_nvformat(struct nvparm_format * new_nvformat, 
                 struct nvparm_format * head_nvformat)
{
struct nvparm_format * curr_format = head_nvformat;

   if (!curr_format || !new_nvformat)
   {
      dprintf("Bad Call to install_nvformat\n");
      return -1;
   }

   while (curr_format->next_format)
      curr_format = curr_format->next_format;

   curr_format->next_format = new_nvformat;
   return 0;
}


/* Include genlist.h for defn of NICHE_DUP_ENTRY, addition, deletion
 * of entries in the generic list. */

#include "genlist.h" 


/* FUNCTION: nv_get_sec_num()
 * 
 * Get the index for the corresponding section.
 *
 * PARAM1: char *buf    - buf points to a section name. 
 * PARAM2: struct nv_sectioninfo *sec - array contain info about sections
 * PARAM3: int slen     - len of sec[]
 *
 * RETURNS: Index to section array or NOT_A_SECTION 
 */

int
nv_get_sec_num(char * buf, struct nv_sectioninfo *sec, int slen)
{
   int   index,sec_index=NOT_A_SECTION;

   for (index=0; index < slen ; index++ )
   {
      if (strncmp(buf, sec[index].name, strlen(sec[index].name)) == 0)
      {
         sec_index = index ;
         break;
      }
   }
   if ( index == slen )  /* Match not found */
   {
      /* There is some unknown/disabled section. Ignore it */
      sec_index = NOT_A_SECTION ;
   }
   return sec_index ;
}

/* FUNCTION: nv_read_parse()
 * 
 * Read in the non-volative parameters from a disk or flash file.
 *
 * PARAM1: char *fname  - name of file to be read
 * PARAM2: struct nv_sectioninfo *sec - list of sections and their info.
 * PARAM3: int slen  - len of sec[] array
 *
 * RETURNS: SUCCESS or error number

 * ALGORITHM : Following algo. will be used for parse the whole NV file.
 *
 * 1. Read the whole file line by line.
 * 2. If there is any error, then return with error code
 * 3. For each line read
 *    a. skip if it is blank line or a comment line
 *    b. If it starts with "[", then update sec_index. "sec_index" is
 *       an index into the sec[] array. Initially it is set to
 *       NOT_A_SECTION. When a match occurs, it will index the respective
 *       section in sec array. As in C language, indices for 
 *       sec[] start with 0.
 *    c. If it doen't start with "[", check sec_index
 *       a. If sec_index is NOT_A_SECTION, then continue
 *       b. Otherwise call the parse_routine for the section indexed
 *          by sec_index.
 * 4. Close the file and return.
 *
 */

/* to be able to read long lines of ipfilter.nv, defined NV_LONGLINE */
#define NV_LONGLINE  (NV_LINELENGTH+20)

int nv_read_parse(char *fname, struct nv_sectioninfo *sec,int slen)
{
   char * cp;                    /* scratch */
   FILE * fp;                    /* input file */
   unsigned line;                 /* current line in input file */
   int sec_index= NOT_A_SECTION; /* init to not in a list */
   int ret_code = SUCCESS;
   static char linebuf[NV_LONGLINE]; /* scratch local buffer for file reading */

   fp = (FILE *)nv_fopen(fname, "r");
   if (!fp)
   {
      return ENP_NOFILE;
   }

   line = 0;
   while (nv_fgets(linebuf, NV_LONGLINE, fp) == linebuf)
   {
      line++;      
      if ((linebuf[0] == '#') || /* see if line is commented out */
          (linebuf[0] == ' ') ||  /* or starts with whitespace */
          (linebuf[0] == '\t') || /* or starts with whitespace */
          (linebuf[0] == '\n') || /* or is empty */
          (linebuf[0] == '\r'))   /* or is empty */
      {
         continue;
      }
      cp = strchr(linebuf, '\n');   /* find possible linefeed */
      if (cp)     /* if linefeed exists, null it over */
         *cp = '\0';

      cp = strchr(linebuf, '#');    /* find possible in-line comment */
      if (cp)     /* if comment char exists, null it over */
         *cp = '\0';

      if (linebuf[0] == '[')
      {
         sec_index = nv_get_sec_num(&linebuf[1],sec,slen);
         continue;
      }

      /* fall to here if linebuf should contain a record */
      if (sec_index == NOT_A_SECTION)
         continue;
      else 
      {
         ret_code = sec[sec_index].parse_func(linebuf);
         if (ret_code != SUCCESS )
         {
            dprintf("Error #%d (line %d of %s)\n",ret_code,line,fname);
            if ( ret_code == NICHE_DUP_ENTRY )
               continue;
            else
            {
               nv_fclose(fp);
               return ret_code;
            } 
         }
      }
   }

   if (line < 1)
   {
      ret_code  = ENP_PARAM ;
   }

   nv_fclose(fp);
   return ret_code;
}

#ifdef USE_GENLIST

/* FUNCTION: nv_add_entry()
 * 
 * Add an entry to a table. The table is found from
 * the index'th entry in sec[].  
 *
 * Here is the generic idea
 * - Same parsing function is used to parse an entry from NV file
 *   and parse the user command on cmdline. 
 * - Hence the same nv_sectioninfo struct is used to reference the
 *   parsing function.
 * - When user enters a cmd (on cmdline) to add an entry to table,
 *   nv_add_entry() can be used to parse the info and add it to the table.
 * - Similarly, user enters a cmd (on cmdline) to del an entry in table,
 *   nv_del_entry() can be used to delete the entry in the table.
 *
 * PARAM1: void *pio - Pointer to GenericIO structure
 * PARAM2: int index - Index in sec[] array.
 * PARAM3: struct nv_sectioninfo * sec
 *
 * RETURNS: SUCCESS or error code.
 */

int 
nv_add_entry(void * pio, int index, struct nv_sectioninfo *sec)
{
   char *   cp;
   int   err;

   if ( index == NOT_A_SECTION )
   {
      ns_printf(pio,"Could not find info about table. Can't process cmd.\n");
      return -1;
   }

   cp = nextarg( ((GEN_IO)pio)->inbuf );  /* see if user put parm on cmd line */
   if (!*cp)
   {
      if ( sec[index].usage_func )
         sec[index].usage_func(pio);
      return -1;
   }

   err=sec[index].parse_func(cp);

   if ( err == SUCCESS )
      return SUCCESS;
   else
   {
      ns_printf(pio,"Error #%d\n",err);
      return -1;
   }
}

/* FUNCTION: nv_del_entry()
 * 
 * Delete an entry (row) from a table. The information
 * about the table is given by the entry in section table.
 *
 * The information on the cmdline, is the index. So if its "5", then
 * index is 5 (0 based index) and hence the 6th entry/row in the table
 * is to be deleted.
 *
 * Example :
 * Say for deleting an entry in SNMPv3 group table, user enters the
 * following command on the command line
 *    "v3delgroup 3"
 * v3_del_group() gets called with inbuf member of pio pointing to the cmdline.
 * It calls nv_del_entry(). It passes a ptr to sec[] and corresponding
 * index into this array. Hence sec[index] contains information about
 * the table (SNMpv3 group table in this case) to be modified. 
 * nv_del_entry() parses the cmdline, that is pio->inbuf, and then 
 * calls niche_del() to delete the 4th entry/row in the SNMPv3 group table.
 *
 * PARAM1: void *pio
 * PARAM2: int index
 * PARAM3: struct nv_sectioninfo * sec
 * 1. Pointer to GenericIO structure
 * 2. Index in sec[] array.
 * 3. Pointer to sec[] array
 *
 * RETURNS: SUCCESS or error code.
 */

int 
nv_del_entry(void * pio, int index, struct nv_sectioninfo *sec)
{
   GEN_STRUCT entry;
   char *   cp;

   if ( index == NOT_A_SECTION )
   {
      ns_printf(pio,"Could not find info about table. Can't process cmd.\n");
      return -1;
   }

   cp = nextarg( ((GEN_IO)pio)->inbuf );  /* see if user put parm on cmd line */
   if (!*cp)
   {
      if ( sec[index].usage_func )
         sec[index].usage_func(pio);
      return -1;
   }

   entry = niche_list_getat((NICHELIST)sec[index].list,atoi(cp));
   if ( entry )
   {
      niche_del((NICHELIST)sec[index].list,entry);
      return SUCCESS;
   }
   else
      return -1;
}

/* FUNCTION: nv_del_entry_byid()
 * 
 * Delete an entry (row) from a table. The information
 * about the table is given by the entry in section table.
 *
 * The information on the cmdline is "id" of entry.
 * Hence the entry in the table with similar "id" needs to be deleted.
 *
 * PARAM1: void *pio
 * PARAM2: int index
 * PARAM3: struct nv_sectioninfo * sec
 * 1. Pointer to GenericIO structure
 * 2. Index in sec[] array.
 * 3. Pointer to sec[] array
 *
 * RETURNS: SUCCESS or error code.
 */

int 
nv_del_entry_byid(void * pio, int index, struct nv_sectioninfo *sec)
{
   char *   cp;

   if ( index == NOT_A_SECTION )
   {
      ns_printf(pio,"Could not find info about table. Can't process cmd.\n");
      return -1;
   }

   cp = nextarg( ((GEN_IO)pio)->inbuf );  /* see if user put parm on cmd line */
   if (!*cp)
   {
      if ( sec[index].usage_func )
         sec[index].usage_func(pio);
      return -1;
   }

   return niche_del_id((NICHELIST)sec[index].list,atoi(cp));
}


#endif /* USE_GENLIST */

#endif   /* INCLUDE_NVPARMS */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?