Exemplo n.º 1
0
int strparsecword(char **inbuf, char *outbuf, size_t maxlen)
{
	char buffer[maxlen];
	char *buf = buffer;
	char *c = *inbuf;
	char *start;

	for (; *c != 0 && isspace(*c); c++);

	if (*c == 0) 
		return 0;
	if (strlen(*inbuf) > maxlen)
		return 0;
	for (; *c != 0; c++)
	{
		if (*c == '"')
		{
			start = c+1;
			for (c++; *c != 0 && *c != '"'; c++)
			{
				if (*c == '\\')
				{
					c++;
					if (*c == 0)
						return 0;
				}
			}
			if (*c == 0)
				return 0;
			/* dequote the string */
			strunescape(start, buf, (int) (c - start + 1), 1);
			buf += strlen(buf);
			continue;
		}
		
		if (c != *inbuf && isspace(*c) != 0 && isspace(c[-1]) != 0)
			continue;
		if (isspace(*c) == 0)
			return 0;
		*buf++ = ' ';
	}
	*buf = 0;
	strncpy(outbuf, buffer, maxlen);
	*inbuf = c;

	return 1;
}
Exemplo n.º 2
0
int strparsequoteword(char **inbuf, char *outbuf, size_t maxlen)
{
	char *start = *inbuf;
	char *c;

	/* skip ws, return if empty */
	for (; *start != 0 && isspace(*start); start++); 
	if (*start == 0)
		return 0;

	c = start;
	
	for (; *c != 0 && isspace(*c) == 0; c++)
	{
		if (*c == '"')
		{
			for (c++; *c != 0 && *c != '"'; c++)
			{
				if (*c == '\\')
				{
					c++;
					if (*c == 0)
						return 0;
				}
			}
			if (*c == 0)
				return 0;
		}
		if (*c == '[')
		{
			for (c++; *c != 0 && *c != ']'; c++);
			if (*c == 0)
				return 0;
		}
	}

	/* dequote the string */
	strunescape(start, outbuf, (int) (c - start + 1), 1);

	/* skip trailing spaces */
	for (; *c != 0 && isspace(*c); c++);
	*inbuf = c;

	return 1;
}
Exemplo n.º 3
0
char *unescapestr(const char *in)
{
	static size_t buflen = 0;
	static char *buf = NULL;
	size_t inlen;

	if (in == 0) return 0;

	inlen = strlen(in) + 1;
	/* This is slightly too much memory, because each escaped newline
	 * will be unescaped; but an upper bound is fine.
	 */
	if (buflen < inlen) {
		buflen = inlen;
		buf = realloc(buf, buflen * sizeof *buf);
		if (!buf)
			DIE("Out of memory");
	}

	strunescape(in, buf, buflen, 0);
	return buf;
}
Exemplo n.º 4
0
static int tbl_config_table (oconfig_item_t *ci)
{
	tbl_t *tbl;

	int status = 0;
	size_t i;

	if ((1 != ci->values_num)
			|| (OCONFIG_TYPE_STRING != ci->values[0].type)) {
		log_err ("<Table> expects a single string argument.");
		return 1;
	}

	tbl = realloc (tables, (tables_num + 1) * sizeof (*tables));
	if (NULL == tbl) {
		char errbuf[1024];
		log_err ("realloc failed: %s.",
				sstrerror (errno, errbuf, sizeof (errbuf)));
		return -1;
	}

	tables = tbl;
	++tables_num;

	tbl = tables + tables_num - 1;
	tbl_setup (tbl, ci->values[0].value.string);

	for (i = 0; i < ((size_t) ci->children_num); ++i) {
		oconfig_item_t *c = ci->children + i;

		if (0 == strcasecmp (c->key, "Separator"))
			tbl_config_set_s (c->key, &tbl->sep, c);
		else if (0 == strcasecmp (c->key, "Instance"))
			tbl_config_set_s (c->key, &tbl->instance, c);
		else if (0 == strcasecmp (c->key, "Result"))
			tbl_config_result (tbl, c);
		else
			log_warn ("Ignoring unknown config key \"%s\" "
					"in <Table %s>.", c->key, tbl->file);
	}

	if (NULL == tbl->sep) {
		log_err ("Table \"%s\" does not specify any separator.", tbl->file);
		status = 1;
	} else {
		strunescape (tbl->sep, strlen (tbl->sep) + 1);
	}

	if (NULL == tbl->instance) {
		tbl->instance = sstrdup (tbl->file);
		replace_special (tbl->instance, strlen (tbl->instance));
	}

	if (NULL == tbl->results) {
		log_err ("Table \"%s\" does not specify any (valid) results.",
				tbl->file);
		status = 1;
	}

	if (0 != status) {
		tbl_clear (tbl);
		--tables_num;
		return status;
	}

	for (i = 0; i < tbl->results_num; ++i) {
		tbl_result_t *res = tbl->results + i;
		size_t j;

		for (j = 0; j < res->instances_num; ++j)
			if (res->instances[j] > tbl->max_colnum)
				tbl->max_colnum = res->instances[j];

		for (j = 0; j < res->values_num; ++j)
			if (res->values[j] > tbl->max_colnum)
				tbl->max_colnum = res->values[j];
	}
	return 0;
} /* tbl_config_table */
Exemplo n.º 5
0
static int tbl_config_table(oconfig_item_t *ci) {
  if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) {
    log_err("<Table> expects a single string argument.");
    return 1;
  }

  tbl_t *tbl = realloc(tables, (tables_num + 1) * sizeof(*tables));
  if (tbl == NULL) {
    log_err("realloc failed: %s.", STRERRNO);
    return -1;
  }

  tables = tbl;

  tbl = tables + tables_num;
  tbl_setup(tbl, ci->values[0].value.string);

  for (int i = 0; i < ci->children_num; i++) {
    oconfig_item_t *c = ci->children + i;

    if (strcasecmp(c->key, "Separator") == 0)
      cf_util_get_string(c, &tbl->sep);
    else if (strcasecmp(c->key, "Plugin") == 0)
      cf_util_get_string(c, &tbl->plugin_name);
    else if (strcasecmp(c->key, "Instance") == 0)
      cf_util_get_string(c, &tbl->instance);
    else if (strcasecmp(c->key, "Result") == 0)
      tbl_config_result(tbl, c);
    else
      log_warn("Ignoring unknown config key \"%s\" "
               "in <Table %s>.",
               c->key, tbl->file);
  }

  int status = 0;
  if (tbl->sep == NULL) {
    log_err("Table \"%s\" does not specify any separator.", tbl->file);
    status = 1;
  } else {
    strunescape(tbl->sep, strlen(tbl->sep) + 1);
  }

  if (tbl->instance == NULL) {
    tbl->instance = sstrdup(tbl->file);
    replace_special(tbl->instance, strlen(tbl->instance));
  }

  if (tbl->results == NULL) {
    assert(tbl->results_num == 0);
    log_err("Table \"%s\" does not specify any (valid) results.", tbl->file);
    status = 1;
  }

  if (status != 0) {
    tbl_clear(tbl);
    return status;
  }

  for (size_t i = 0; i < tbl->results_num; ++i) {
    tbl_result_t *res = tbl->results + i;

    for (size_t j = 0; j < res->instances_num; ++j)
      if (res->instances[j] > tbl->max_colnum)
        tbl->max_colnum = res->instances[j];

    for (size_t j = 0; j < res->values_num; ++j)
      if (res->values[j] > tbl->max_colnum)
        tbl->max_colnum = res->values[j];
  }

  tables_num++;
  return 0;
} /* tbl_config_table */