Exemple #1
0
int parse_cmdline(struct cfgdata_t *cfgdata)
{
	FILE *f;
	char *c;
	char *keyword;
	char *value;
	char line[COMMAND_LINE_SIZE];

	/* Open /proc/cmdline and read cmdline */
	f = fopen("/proc/cmdline", "r");
	if (NULL == f) {
		log_msg(lg, "Can't open /proc/cmdline: %s", ERRMSG);
		return -1;
	}

	if ( NULL == fgets(line, sizeof(line), f) ) {
		log_msg(lg, "Can't read /proc/cmdline: %s", ERRMSG);
		fclose(f);
		return -1;
	}

	fclose(f);

	log_msg(lg, "Kernel cmdline: %s", line);

	c = line;
	/* Split string to words */
	while ( NULL != (keyword = get_word(c, &c)) ) {
		if ( ('\0' == keyword[0]) || ('#' == keyword[0]) ) {
			/* Skip comment or empty line */
			continue;
		}

		*c = '\0';	/* NULL-terminate keyword-value pair */
		++c;

		/* Try to split line up to key and value */
		value = strchr(keyword, '=');
		if (NULL != value) {	/* '=' was found. We have value */
			/* Split string to keyword and value */
			*value = '\0';
			++value;
		}

		/* Process keyword and value */
		process_keyword(CFG_CMDLINE, cfgdata, keyword, value);
	}

	return 0;
}
Exemple #2
0
/* NOTE: It will not clean cfgdata before parsing, do it yourself */
int parse_cfgfile(char *path, struct cfgdata_t *cfgdata)
{
	int linenr = 0;
	FILE *f;
	char *c;
	char *keyword;
	char *value;
	char line[256];

	/* Open the config file */
	f = fopen(path, "r");
	if (NULL == f) {
		log_msg(lg, "+ can't open config file: %s", ERRMSG);
		return -1;
	}

	/* Read config file line by line */
	while (fgets(line, sizeof(line), f)) {
		++linenr;
		/* Skip white-space from beginning */
		keyword = ltrim(line);

		if ( ('\0' == keyword[0]) || ('#' == keyword[0]) ) {
			/* Skip comment or empty line */
			continue;
		}
		/* Try to split line up to key and value */
		c = strchr(keyword, '=');
		if (NULL != c) {	/* '=' was found. We have value */
			/* Split string to keyword and value */
			*c = '\0';
			++c;

			value = trim(c);	/* Strip white-space from value */
		} else {
			value = NULL;
		}
		c = rtrim(keyword);	/* Strip trailing white-space from keyword */
		*(c+1) = '\0';

		/* Process keyword and value */
		if (-1 == process_keyword(CFG_FILE, cfgdata, keyword, value)) {
			log_msg(lg, "Can't parse keyword '%s'", keyword);
		}
	}

	fclose(f);
	return 0;
}
Exemple #3
0
// Looks at a string vector and parse out the node index values inside
// { } into an integer list 
static int process_strvec(vector<string> *raw_strvec, 
                          list<int> *intlist, int ntwk_size)
{

      vector<string> str_vec;


      boost::smatch str_matches;
      size_t size_max = ntwk_size;
      int error;

      error = extract_idxstr(raw_strvec, str_vec);
      if (error)
          return error;

      // Look for any special keyword
      vector<string>::iterator it = str_vec.begin();
      if ( boost::regex_match(*it, str_matches, keyword_syntax) )
      {
            // Process Keyword
            process_keyword(*it, intlist, ntwk_size);
            ++it;
      }

      unsigned int idx;
      string idx_str;

      // NEED TO LOOP THROUGH AND CHECK FOR RANGE. 
      while(it != str_vec.end())
      {
          idx_str = *it;

          if ( boost::regex_match(idx_str, str_matches, range_specifier) )
          {
                // Process range syntax
                interpret_range(idx_str, intlist);

          }
          else if (boost::regex_match(idx_str, str_matches, valid_idx) )
          {
              // Process a regular single integer index
              idx = boost::lexical_cast<int>(idx_str);

              if ( idx <  size_max )
              {
                    intlist->push_back(idx);
              }
              else
              {
                  cerr << "ERROR node index out of bounds " << endl
                       << "Index parsed: " << idx  
                       << "Max allowed: " << size_max <<endl;
                  return ERROR;
              }
          }
          else
          {
              cerr << "ERROR: Invalid node index - " << idx_str << endl;
              return ERROR;
          }
          ++it;
    }
   
    // Sort the list from smallest to largest.
    intlist->sort(lessThan);
    // Remove duplicates
    intlist->unique(); 
    return SUCCESS;
  
}