Ejemplo n.º 1
0
int
parse_token(const char *text, const char *delims, OnTokenFunc on_token, void *ctx)
{
	int count = 0;
	char *copy_text = strdup(text);
	char *ptr, *token = copy_text;
	for(ptr=copy_text; *ptr!='\0'; ptr++) {
		switch(state) {
			case STAT_INIT:
			case STAT_OUT:
				if(!IS_DELIM(*ptr, delims)) {
					token = ptr;
					state = STAT_IN;
				}
				break;
			case STAT_IN:
				if(IS_DELIM(*ptr, delims)) {
					*ptr = '\0';
					on_token(ctx, count++, token);
					state = STAT_OUT;
				}
				break;
			default:
				break;
		}
	}
	if(state == STAT_IN)
		on_token(ctx, count++, token);
	free(copy_text);
	return count;
}
Ejemplo n.º 2
0
char* nsCRT::strtok(char* string, const char* delims, char* *newStr)
{
  NS_ASSERTION(string, "Unlike regular strtok, the first argument cannot be null.");

  char delimTable[DELIM_TABLE_SIZE];
  PRUint32 i;
  char* result;
  char* str = string;

  for (i = 0; i < DELIM_TABLE_SIZE; i++)
    delimTable[i] = '\0';

  for (i = 0; delims[i]; i++) {
    SET_DELIM(delimTable, static_cast<PRUint8>(delims[i]));
  }
  NS_ASSERTION(delims[i] == '\0', "too many delimiters");

  // skip to beginning
  while (*str && IS_DELIM(delimTable, static_cast<PRUint8>(*str))) {
    str++;
  }
  result = str;

  // fix up the end of the token
  while (*str) {
    if (IS_DELIM(delimTable, static_cast<PRUint8>(*str))) {
      *str++ = '\0';
      break;
    }
    str++;
  }
  *newStr = str;

  return str == result ? NULL : result;
}
Ejemplo n.º 3
0
static int parse_token(const char* text, const char* delims, OnTokenFunc on_token, void* ctx, char** ret_copy_text)
{
	enum _State
	{
		STAT_INIT,
		STAT_IN,
		STAT_OUT,
	}state = STAT_INIT;

	int   count		= 0;
	char* copy_text = ftk_strdup(text);
	char* p			= copy_text;
	char* token		= copy_text;

	for(p = copy_text; *p != '\0'; p++)
	{
		switch(state)
		{
			case STAT_INIT:
			case STAT_OUT:
			{
				if(!IS_DELIM(*p))
				{
					token = p;
					state = STAT_IN;
				}
				break;
			}
			case STAT_IN:
			{
				if(IS_DELIM(*p))
				{
					*p = '\0';
					on_token(ctx, count++, token);
					state = STAT_OUT;
				}
				break;
			}
			default:break;
		}
	}

	if(state == STAT_IN)
	{
		on_token(ctx, count++, token);
	}

	on_token(ctx, -1, NULL);

	if(ret_copy_text != NULL)
	{
		*ret_copy_text = copy_text;
	}
	else
	{
		FTK_FREE(copy_text);
	}

	return count;
}
Ejemplo n.º 4
0
Archivo: strings.c Proyecto: mej/libast
/* Returns the number of words in str, for use with get_word() and get_pword().  "..." counts as 1 word. */
unsigned long
spiftool_num_words(const spif_charptr_t str)
{
    register unsigned long cnt = 0;
    char delim = 0;
    register unsigned long i;

    ASSERT_RVAL(str != (spif_ptr_t) NULL, (unsigned long) -1);
    for (i = 0; str[i] && IS_DELIM(str[i]); i++);
    for (; str[i]; cnt++) {
        switch (str[i]) {
          case '\"':
              delim = '\"';
              i++;
              break;
          case '\'':
              delim = '\'';
              i++;
              break;
          default:
              delim = 0;
        }
        for (; str[i] && !IS_DELIM(str[i]); i++);
        switch (str[i]) {
          case '\"':
          case '\'':
              i++;
              break;
        }
        for (; str[i] && isspace(str[i]); i++);
    }

    D_STRINGS(("num_words() returning %lu\n", cnt));
    return (cnt);
}
Ejemplo n.º 5
0
static int	get_file_name_len(const char *str)
{
  int		len;
  const char	*save;

  save = str;
  while (IS_SPACE(*str))
    ++str;
  if (!(*str) || (IS_DELIM(*str)))
    return (return_error(MISS_NAME_DIR, END_CHAR));
  while ((*str) && !(IS_SPACE(*str)) && !(IS_DELIM(*str)))
    {
      if (IS_QUOTE(*str))
	{
	  if ((len = pass_quote(str)) == -1)
	    return (return_error(UNMATCH_QUOTE, *str));
	  str += len;
	}
      else if (*str == B_SLASH)
	{
	  if (!(*(++str)))
	    return (return_error(MISS_SLASH, END_CHAR));
	}
      ++str;
    }
  return (str - save);
}
Ejemplo n.º 6
0
Archivo: strings.c Proyecto: mej/libast
spif_charptr_t 
spiftool_get_word(unsigned long index, const spif_charptr_t str)
{
    spif_charptr_t tmpstr;
    char delim = 0;
    register unsigned long i, j, k;

    ASSERT_RVAL(str != (spif_ptr_t) NULL, (spif_ptr_t) NULL);
    k = strlen((char *) str) + 1;
    if (!(tmpstr = (spif_charptr_t)MALLOC(k))) {
        libast_print_error("get_word(%lu, %s):  Unable to allocate memory -- %s.\n", index, str, strerror(errno));
        return ((spif_charptr_t) NULL);
    }
    *tmpstr = 0;
    for (i = 0, j = 0; j < index && str[i]; j++) {
        for (; isspace(str[i]); i++);
        switch (str[i]) {
          case '\"':
              delim = '\"';
              i++;
              break;
          case '\'':
              delim = '\'';
              i++;
              break;
          default:
              delim = 0;
        }
        for (k = 0; str[i] && !IS_DELIM(str[i]);) {
            if (str[i] == '\\') {
                if (str[i + 1] == '\'' || str[i + 1] == '\"') {
                    i++;
                }
            }
            tmpstr[k++] = str[i++];
        }
        switch (str[i]) {
          case '\"':
          case '\'':
              i++;
              break;
        }
        tmpstr[k] = 0;
    }

    if (j != index) {
        FREE(tmpstr);
        D_STRINGS(("get_word(%lu, %s) returning NULL.\n", index, str));
        return ((spif_charptr_t) NULL);
    } else {
        tmpstr = (spif_charptr_t) REALLOC(tmpstr, strlen((char *) tmpstr) + 1);
        D_STRINGS(("get_word(%lu, %s) returning \"%s\".\n", index, str, tmpstr));
        return (tmpstr);
    }
}
Ejemplo n.º 7
0
int get_token(str_t* str, char** buf, IS_DELIM_FUNC delim_func, const char* delim)
{
	if(buf==NULL || *buf==NULL) return 0;

	char* p = *buf;
	char* p_start;

	while(*p!='\0' && IS_DELIM(delim_func, delim, *p)) p++;

	if(*p == '\0') return 0;
	
	p_start = p;
	p++;
	while(*p!='\0' && !IS_DELIM(delim_func, delim, *p)) p++;

	str->data = p_start;
	str->len = p - p_start;
	*p = '\0';
	*buf = p + 1;

	return 1;
}
Ejemplo n.º 8
0
char*
nsCRT::strtok(char* aString, const char* aDelims, char** aNewStr)
{
  NS_ASSERTION(aString,
               "Unlike regular strtok, the first argument cannot be null.");

  char delimTable[DELIM_TABLE_SIZE];
  uint32_t i;
  char* result;
  char* str = aString;

  for (i = 0; i < DELIM_TABLE_SIZE; ++i) {
    delimTable[i] = '\0';
  }

  for (i = 0; aDelims[i]; i++) {
    SET_DELIM(delimTable, static_cast<uint8_t>(aDelims[i]));
  }
  NS_ASSERTION(aDelims[i] == '\0', "too many delimiters");

  // skip to beginning
  while (*str && IS_DELIM(delimTable, static_cast<uint8_t>(*str))) {
    str++;
  }
  result = str;

  // fix up the end of the token
  while (*str) {
    if (IS_DELIM(delimTable, static_cast<uint8_t>(*str))) {
      *str++ = '\0';
      break;
    }
    str++;
  }
  *aNewStr = str;

  return str == result ? nullptr : result;
}
Ejemplo n.º 9
0
/**
 * @brief Get the sentence of the given text string.
 * @param str A text string.
 * @param n Index number. (0 to ntopt-get_count(str) - 1)
 * @param buf The pointer to a stored buffer.
 * @param siz The size of the stored buffer.
 * @param len The stored string length.
 * @retval !NULL Success. The pointer to the buffer.
 * @retval NULL Failure.
 */
static char *ntopt_get_text(
        const char *str, const int n, char *buf, int siz, int *len)
{
    int cnt = 0;
    int wc = 0;
    char *p = (char *)str;
    *len = 0;
    while (*p) {
        if (!IS_DELIM(*p)) {
            wc++;
            if (wc == 1) {
                if (cnt == n) {
                    char *des = buf;
                    int cc = 0;
                    while (!IS_DELIM(*p)) {
                        cc++;
                        if (siz <= cc) {
                            break;
                        }
                        *des = *p;
                        des++;
                        p++;
                    }
                    *des = '\0';
                    *len = cc;
                    return buf;
                }
                cnt++;
            }
        } else {
            wc = 0;
        }
        p++;
    }
    return (char *)0;
}
Ejemplo n.º 10
0
/**
 * @brief Get the sentence count of the given text string.
 * @param str A text string.
 * @return Count of the given sentence.
 */
static int ntopt_get_count(const char *str)
{
    int cnt = 0;
    int wc = 0;
    char *p = (char *)str;
    while (*p) {
        if (!IS_DELIM(*p)) {
            wc++;
            if (wc == 1) {
                cnt++;
            }
        } else {
            wc = 0;
        }
        p++;
    }
    return cnt;
}
Ejemplo n.º 11
0
/*
 * Parse Supported HF body.
 */
int parse_supported_body(str *body, unsigned int *sup)
{
	register char* p;
	register unsigned int val;
	int len, pos = 0;

	*sup = 0;

	p = body->s;
	len = body->len;

	while (pos < len) {
		/* skip spaces and commas */
		for (; pos < len && IS_DELIM(p); ++pos, ++p);

		val = LOWER_DWORD(READ(p));
		switch (val) {

			/* "path" */
			case _path_:
				if(pos + 4 <= len && IS_DELIM(p+4)) {
					*sup |= F_SUPPORTED_PATH;
					pos += 5; p += 5;
				} else
					goto default_label;
				break;

			/* "gruu" */
			case _gruu_:
				if(pos + 4 <= len && IS_DELIM(p+4)) {
					*sup |= F_SUPPORTED_GRUU;
					pos += 5; p += 5;
				} else
					goto default_label;
				break;

			/* "100rel" */
			case _100r_:
				if ( pos+6 <= len
					 && LOWER_BYTE(*(p+4))=='e' && LOWER_BYTE(*(p+5))=='l'
					 && IS_DELIM(p+6)) {
					*sup |= F_SUPPORTED_100REL;
					pos += SUPPORTED_100REL_LEN + 1;
					p   += SUPPORTED_100REL_LEN + 1;
				} else
					goto default_label;
				break;

			/* "timer" */
			case _time_:
				if ( pos+5 <= len && LOWER_BYTE(*(p+4))=='r'
					 && IS_DELIM(p+5) ) {
					*sup |= F_SUPPORTED_TIMER;
					pos += SUPPORTED_TIMER_LEN + 1;
					p   += SUPPORTED_TIMER_LEN + 1;
				} else
					goto default_label;
				break;

			/* "eventlist" */
			case _even_:
				if ( pos+9 <= len && LOWER_DWORD(READ(p+4))==_tlis_ && LOWER_BYTE(*(p+8))=='t'
					 && IS_DELIM(p+9) ) {
					*sup |= F_SUPPORTED_EVENTLIST;
					pos += SUPPORTED_EVENTLIST_LEN + 1;
					p   += SUPPORTED_EVENTLIST_LEN + 1;
				} else
					goto default_label;
				break;

			/* unknown */
			default:
default_label:
				/* skip element */
				for (; pos < len && !IS_DELIM(p); ++pos, ++p);
				break;
		}
	}

	return 0;
}
Ejemplo n.º 12
0
int cfg_get_token(cfg_token_t* token, cfg_parser_t* st, unsigned int flags)
{
	static int look_ahead = EOF;
	int c;
	enum st state;

	state = ST_S;
	
	token->val.s = token->buf;
	token->val.len = 0;

	if (look_ahead != EOF) {
		c = look_ahead;
		look_ahead = EOF;
	} else {
		READ_CHAR;
	}

	while(c != EOF) {
		switch(state) {
		case ST_S:
			if (flags & CFG_EXTENDED_ALPHA) {
				if (IS_WHITESPACE(c)) {
					     /* Do nothing */
				} else if (IS_ALPHA(c) ||
					   IS_ESCAPE(c) ||
					   IS_DELIM(c)) {
					PUSH(c);
					state = ST_A;
				} else if (IS_QUOTE(c)) {
					state = ST_Q;
				} else if (IS_COMMENT(c)) {
					state = ST_C;
				} else if (IS_EOL(c)) {
					PUSH(c);
					RETURN(c);
				} else {
					ERR("%s:%d:%d: Invalid character 0x%x\n", 
					    st->file, st->line, st->col, c);
					return -1;
				}
			} else {
				if (IS_WHITESPACE(c)) {
					     /* Do nothing */
				} else if (IS_ALPHA(c)) {
					PUSH(c);
					state = ST_A;
				} else if (IS_QUOTE(c)) {
					state = ST_Q;
				} else if (IS_COMMENT(c)) {
					state = ST_C;
				} else if (IS_ESCAPE(c)) {
					state = ST_E;
				} else if (IS_DELIM(c) || IS_EOL(c)) {
					PUSH(c);
					RETURN(c);
				} else {
					ERR("%s:%d:%d: Invalid character 0x%x\n", 
					    st->file, st->line, st->col, c);
					return -1;
				}
			}
			break;

		case ST_A:
			if (flags & CFG_EXTENDED_ALPHA) {
				if (IS_ALPHA(c) ||
				    IS_DELIM(c) ||
				    IS_QUOTE(c)) {
					PUSH(c);
				} else if (IS_ESCAPE(c)) {
					state = ST_AE;
				} else if (IS_COMMENT(c) || IS_EOL(c) || IS_WHITESPACE(c)) {
					look_ahead = c;
					RETURN(CFG_TOKEN_ALPHA);
				} else {
					ERR("%s:%d:%d: Invalid character 0x%x\n", 
					    st->file, st->line, st->col, c);
					return -1;
				}
			} else {
				if (IS_ALPHA(c)) {
					PUSH(c);
				} else if (IS_ESCAPE(c)) {
					state = ST_AE;
				} else if (IS_WHITESPACE(c) ||
					   IS_DELIM(c) ||
					   IS_QUOTE(c) ||
					   IS_COMMENT(c) ||
					   IS_EOL(c)) {
					look_ahead = c;
					RETURN(CFG_TOKEN_ALPHA);
				} else {
					ERR("%s:%d:%d: Invalid character 0x%x\n", 
					    st->file, st->line, st->col, c);
					return -1;
				}
			}
			break;

		case ST_AE:
			if (IS_COMMENT(c) ||
			    IS_QUOTE(c) ||
			    IS_ESCAPE(c)) {
				PUSH(c);
			} else if (c == 'r') {
				PUSH('\r');
			} else if (c == 'n') {
				PUSH('\n');
			} else if (c == 't') {
				PUSH('\t');
			} else if (c == ' ') {
				PUSH(' ');
			} else if (IS_EOL(c)) {
				     /* Do nothing */
			} else {
				ERR("%s:%d:%d: Unsupported escape character 0x%x\n", 
				    st->file, st->line, st->col, c);
				return -1;
			}
			state = ST_A;
			break;

		case ST_Q:
			if (IS_QUOTE(c)) {
				RETURN(CFG_TOKEN_STRING);
			} else if (IS_ESCAPE(c)) {
				state = ST_QE;
				break;
			} else {
				PUSH(c);
			}
			break;

		case ST_QE:
			if (IS_ESCAPE(c) ||
			    IS_QUOTE(c)) {
				PUSH(c);
			} else if (c == 'n') {
				PUSH('\n');
			} else if (c == 'r') {
				PUSH('\r');
			} else if (c == 't') {
				PUSH('\t');
			} else if (IS_EOL(c)) {
				     /* Do nothing */
			} else {
				ERR("%s:%d:%d: Unsupported escape character 0x%x\n", 
				    st->file, st->line, st->col, c);
				return -1;
			}
			state = ST_Q;
			break;

		case ST_C:
			if (IS_ESCAPE(c)) {
				state = ST_CE;
			} else if (IS_EOL(c)) {
				state = ST_S;
				continue; /* Do not read a new char, return EOL */
			} else {
				     /* Do nothing */
			}
			break;

		case ST_CE:
			state = ST_C;
			break;

		case ST_E:
			if (IS_COMMENT(c) ||
			    IS_QUOTE(c) ||
			    IS_ESCAPE(c)) {
				PUSH(c);
				RETURN(c);
			} else if (c == 'r') {
				PUSH('\r');
				RETURN('\r');
			} else if (c == 'n') {
				PUSH('\n');
				RETURN('\n');
			} else if (c == 't') {
				PUSH('\t');
				RETURN('\t');
			} else if (c == ' ') {
				PUSH(' ');
				RETURN(' ');
			} else if (IS_EOL(c)) {
				     /* Escped eol means no eol */
				state = ST_S;
			} else {
				ERR("%s:%d:%d: Unsupported escape character 0x%x\n", 
				    st->file, st->line, st->col, c);
				return -1;
			}
			break;
		}

		READ_CHAR;
	};

	switch(state) {
	case ST_S: 
	case ST_C:
	case ST_CE:
		return 1;

	case ST_A:
		RETURN(CFG_TOKEN_ALPHA);

	case ST_Q:
		ERR("%s:%d:%d: Premature end of file, missing closing quote in"
			" string constant\n", st->file, st->line, st->col);
		return -1;

	case ST_QE:
	case ST_E:
	case ST_AE:
		ERR("%s:%d:%d: Premature end of file, missing escaped character\n", 
		    st->file, st->line, st->col);
		return -1;
	}
	BUG("%s:%d:%d: Invalid state %d\n",
		st->file, st->line, st->col, state);
	return -1;
}
Ejemplo n.º 13
0
short int php_tmpl_pre_parse_config(t_template* tmpl) {
zval		*zparam;

char	*nam, *val;
uint	nam_len, val_len;

uchar	quote;
register char	*p;
register char	*start;

	nam = (char*)emalloc(TMPL_MAX_TAG_LEN); nam_len = 0;
	val = (char*)emalloc(TMPL_MAX_TAG_LEN); val_len = 0;
	MAKE_STD_ZVAL(zparam); array_init(zparam);

	sprintf(nam, "<%s", TMPL_CONFIG_TAG_NAME);
	nam_len = strlen(nam);

	if(!(p = strstrl_ex(ZV(tmpl->original), ZL(tmpl->original), nam, nam_len))) {
		TMPL_PRE_PARSE_CONFIG_CLEANUP;
		return SUCCESS;
	}

	start = p;
	p += nam_len;

	while(1) {

		/* skip delimiters and check for end of the tag */
		while(*p && '>' != *p && IS_DELIM(*p)) p++;
		if(!(*p) || '>' == *p) {	/* end of tag */
			if('>' == *p) {			/* hide this tag from result output */
				for(++p; *p;) *(start++) = *(++p);
				*start = 0;
				ZL(tmpl->original) -= (p-start);
			}
			break;
		}

		/* get parameter name */
		for(nam_len=0; *p && nam_len < TMPL_MAX_TAG_LEN && !IS_DELIM(*p) && '=' != *p; p++) nam[nam_len++] = *p;
		if(!(*p)) break; else nam[nam_len] = 0;

		if('=' != *p) {
			php_error(E_ERROR, "Invalid configuration tag parameter in template (line:%d)", php_tmpl_line_num(tmpl, p));
			TMPL_PRE_PARSE_CONFIG_CLEANUP;
			return FAILURE;
		}

		/* check if the value is quoted and get the value */
		p++;
		quote = IS_QUOTE(*p) ? *(p++) : 0;

		for(val_len=0; *p && val_len < TMPL_MAX_TAG_LEN && quote ? quote != *p : !IS_DELIM(*p); p++) val[val_len++] = *p;
		if(!(*p)) break; else val[val_len] = 0;

		if(quote && quote != *p) {
			php_error(E_ERROR, "Invalid parameter value in configuration tag in template (line:%d)", php_tmpl_line_num(tmpl, p));
			TMPL_PRE_PARSE_CONFIG_CLEANUP;
			return FAILURE;
		}
		if(quote) p++;

		add_assoc_stringl(zparam, nam, val, val_len, 1);
	}

	php_tmpl_process_param_array(tmpl, zparam);

	TMPL_PRE_PARSE_CONFIG_CLEANUP;
	return SUCCESS;
}
Ejemplo n.º 14
0
Archivo: tok.c Proyecto: Limsik/e17
spif_bool_t
spif_tok_eval(spif_tok_t self)
{
    const char *pstr, *delim = NULL;
    spif_str_t tmp;
    char quote;
    size_t len;

    ASSERT_RVAL(!SPIF_TOK_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_STR_ISNULL(self->src), FALSE);

    pstr = (const char *) SPIF_STR_STR(SPIF_STR(self->src));
    len = spif_str_get_len(SPIF_STR(self->src));

    if (!SPIF_STR_ISNULL(self->sep)) {
        delim = (const char *) SPIF_STR_STR(SPIF_STR(self->sep));
    }

    if (!SPIF_LIST_ISNULL(self->tokens)) {
        SPIF_LIST_DEL(self->tokens);
    }
    self->tokens = SPIF_LIST_NEW(dlinked_list);

    /* Before we do anything, skip leading "whitespace." */
    for (; *pstr && IS_DELIM(*pstr); pstr++);

    /* The outermost for loop is where we traverse the string.  Each new
       word brings us back to the top where we resize our string list. */
    for (quote = 0; *pstr; ) {
        tmp = spif_str_new_from_buff(SPIF_CHARPTR(""), len);
        spif_str_clear(tmp, 0);

        /* This for loop is where we process each character. */
        for (; *pstr && (quote || !IS_DELIM(*pstr));) {
            if (*pstr == self->dquote || *pstr == self->quote) {
                /* It's a quote character, so set or reset the quote variable. */
                if (quote) {
                    if (quote == *pstr) {
                        quote = 0;
                    } else {
                        /* It's a single quote inside double quotes, or vice versa.  Leave it alone. */
                        spif_str_append_char(tmp, *pstr);
                    }
                } else {
                    quote = *pstr;
                }
                pstr++;
            } else {
                /* Handle any backslashes that are escaping delimiters or quotes. */
                if ((*pstr == self->escape) && (IS_DELIM(*(pstr + 1)) || IS_QUOTE(*(pstr + 1)))) {
                    /* Incrementing pstr here moves us past the backslash so that the line
                       below will copy the next character to the new token, no questions asked. */
                    pstr++;
                }
                spif_str_append_char(tmp, *pstr++);
            }
        }

        /* Reallocate the new string to be just the right size. */
        spif_str_trim(tmp);
        len -= spif_str_get_len(tmp);

        /* Add it to the list */
        SPIF_LIST_APPEND(self->tokens, tmp);

        /* Move past any trailing "whitespace." */
        for (; *pstr && IS_DELIM(*pstr); pstr++);
    }
    return TRUE;
}
Ejemplo n.º 15
0
Archivo: strings.c Proyecto: mej/libast
spif_charptr_t *
spiftool_split(const spif_charptr_t delim, const spif_charptr_t str)
{
    spif_charptr_t *slist;
    register spif_charptr_t pstr;
    register spif_charptr_t pdest;
    char quote = 0;
    unsigned short cnt = 0;
    unsigned long len;

    REQUIRE_RVAL(str != NULL, (spif_charptr_t *) NULL);

    if (!(slist = (spif_charptr_t *)MALLOC(sizeof(spif_charptr_t)))) {
        libast_print_error("split():  Unable to allocate memory -- %s\n", strerror(errno));
        return ((spif_charptr_t *) NULL);
    }

    /* Before we do anything, skip leading "whitespace." */
    for (pstr = (spif_charptr_t) str; *pstr && IS_DELIM(*pstr); pstr++);

    /* The outermost for loop is where we traverse the string.  Each new
       word brings us back to the top where we resize our string list. */
    for (; *pstr; cnt++) {
        /* First, resize the list to two bigger than our count.  Why two?
           One for the string we're about to do, and one for a trailing NULL. */
        if (!(slist = (spif_charptr_t *)REALLOC(slist, sizeof(spif_charptr_t) * (cnt + 2)))) {
            libast_print_error("split():  Unable to allocate memory -- %s\n", strerror(errno));
            return ((spif_charptr_t *) NULL);
        }

        /* The string we're about to create can't possibly be larger than the remainder
           of the string we have yet to parse, so allocate that much space to start. */
        len = strlen((char *) pstr) + 1;
        if (!(slist[cnt] = (spif_charptr_t)MALLOC(len))) {
            libast_print_error("split():  Unable to allocate memory -- %s.\n", strerror(errno));
            return ((spif_charptr_t *) NULL);
        }
        pdest = slist[cnt];

        /* This for loop is where we process each character. */
        for (; *pstr && (quote || !IS_DELIM(*pstr));) {
            if (*pstr == '\"' || *pstr == '\'') {
                /* It's a quote character, so set or reset the quote variable. */
                if (quote) {
                    if (quote == *pstr) {
                        quote = 0;
                    } else {
                        /* It's a single quote inside double quotes, or vice versa.  Leave it alone. */
                        *pdest++ = *pstr++;
                    }
                } else {
                    quote = *pstr;
                }
                pstr++;
            } else {
                /* Handle any backslashes that are escaping delimiters or quotes. */
                if ((*pstr == '\\') && (IS_DELIM(*(pstr + 1)) || IS_QUOTE(*(pstr + 1)))) {
                    /* Incrementing pstr here moves us past the backslash so that the line
                       below will copy the next character to the new token, no questions asked. */
                    pstr++;
                }
                *pdest++ = *pstr++;
            }
        }
        /* Add the trailing \0 to terminate the new string. */
        *pdest = 0;

        /* Reallocate the new string to be just the right size. */
        len = strlen((char *) slist[cnt]) + 1;
        slist[cnt] = (spif_charptr_t) REALLOC(slist[cnt], len);

        /* Move past any trailing "whitespace." */
        for (; *pstr && IS_DELIM(*pstr); pstr++);
    }
    if (cnt == 0) {
        FREE(slist);
        return NULL;
    } else {
        /* The last element of slist[] should be NULL. */
        slist[cnt] = 0;
        return slist;
    }
}
Ejemplo n.º 16
0
// !! this function modifies data pointed with sCmdLine parameter!
size_t srvParseCmdLine(cEnvironmentHelper* pEnvironmentHelper, tstring sCmdLine, tcstring sDefaultDir, tcstring* params, size_t max_params_count, bool bInterpreteur)
{
	assert((params && max_params_count) || (!params && !max_params_count));
	bool bCMD = bInterpreteur;
	size_t params_count = 0;
	tstring pwchPtr = sCmdLine;
	tstring param;
	tchar wc;
	if (sCmdLine == NULL)
		return 0;
	if (!params && !max_params_count)
		max_params_count = SIZE_MAX;
	wc = tstrchar(pwchPtr);
	while (wc>0 && wc<=' ')
		wc = NEXT_CHAR();
	sCmdLine = pwchPtr;
	if (wc != '\"') // first param not quoted
	{
		wc = tstrchar(pwchPtr);
		while (wc)
		{
			// find delimiter
			while (wc != 0 && !IS_DELIM(wc, bCMD))
				wc = NEXT_CHAR();
			tstrchar(pwchPtr) = 0;
			tstring file = pEnvironmentHelper->PathFindExecutable(sCmdLine, sDefaultDir); // check long file name without quotes
			tstrchar(pwchPtr) = wc;
			if (file)
			{
				if (params)
				{
					params[0] = sCmdLine;
					tstrchar(pwchPtr) = 0;
				}
				params_count++;
				sCmdLine = pwchPtr;
				if (wc) // we have more args
					sCmdLine++;
				tstrfree(file);
				break;
			}
			if (wc)
				wc = NEXT_CHAR();
		}
	}
	pwchPtr = sCmdLine;
	while (params_count < max_params_count)
	{
		wc = tstrchar(pwchPtr);
		
		// skip delimiters at start
		while (wc != 0 && IS_DELIM(wc, bCMD))
			wc = NEXT_CHAR();
		
		if (wc == 0) // end reached
			break; 

		if (wc == '\"') // quoted param
		{
			wc = NEXT_CHAR();
			if (bCMD && wc == '\"')
				wc = NEXT_CHAR();
			
			if (wc == 0) // end reached
				break;

			param = pwchPtr;
			while (wc != 0 && wc != '\"')
				wc = NEXT_CHAR();
		}
		else
		{
			//if (wc != '/') // if not switch
			param = pwchPtr;
			// modified by Sobko was: while (wc != 0 && !IS_DELIM(wc) && wc != '\"')
			while (wc != 0 && !IS_DELIM(wc, bCMD) && wc != '\"')
				wc = NEXT_CHAR();
		}
		if (param != pwchPtr)
		{
			if (params)
			{
				params[params_count] = param;
				tstrchar(pwchPtr) = 0; // zero terminate parameter
			}
			if (params_count == 0)
			{
				tchar c = tstrchar(pwchPtr);
				tstrchar(pwchPtr) = 0; // zero terminate parameter
				if (tstring file = pEnvironmentHelper->PathFindExecutable(param, sDefaultDir)) // check long file name without quotes
				{
					if (srvComparePath(pEnvironmentHelper, file, _T("%ComSpec%")))
						bCMD = true;
					tstrfree(file);
				}
				tstrchar(pwchPtr) = c; // restore terminate parameter
			}
			params_count++;
		}

		if (wc == 0) // end reached
			break;
		
		wc = NEXT_CHAR();
	}

	return params_count;
}
Ejemplo n.º 17
0
/*!
 * Parse Supported HF body.
 */
static inline int parse_supported_body(str *body, unsigned int *sup)
{
	register char* p;
	register unsigned int val;
	int len, pos = 0;

	*sup = 0;

	p = body->s;
	len = body->len;

	while (pos < len) {
		/* skip spaces and commas */
		for (; pos < len && IS_DELIM(p); ++pos, ++p);

		val = LOWER_DWORD(READ(p));
		switch (val) {

			/* "path" */
			case _path_:
				if(pos + 4 <= len && IS_DELIM(p+4)) {
					*sup |= F_SUPPORTED_PATH;
					pos += 5; p += 5;
				}
				break;

			/* "100rel" */
			case _100r_:
				if ( pos+6 <= len
					 && LOWER_BYTE(*(p+4))=='e' && LOWER_BYTE(*(p+5))=='l'
					 && IS_DELIM(p+6)) {
					*sup |= F_SUPPORTED_100REL;
					pos += SUPPORTED_100REL_LEN + 1;
					p   += SUPPORTED_100REL_LEN + 1;
				}
				break;

			/* "timer" */
			case _time_:
				if ( pos+5 <= len && LOWER_BYTE(*(p+4))=='r'
					 && IS_DELIM(p+5) ) {
					*sup |= F_SUPPORTED_TIMER;
					pos += SUPPORTED_TIMER_LEN + 1;
					p   += SUPPORTED_TIMER_LEN + 1;
				}
				break;

			/* extra supported or unknown */
			default:
				if(pos+SUPPORTED_EVENTLIST_LEN<=len
						&& strncasecmp(p, SUPPORTED_EVENTLIST_STR,
							SUPPORTED_EVENTLIST_LEN)==0
						&& IS_DELIM(p+9) ) {
					*sup |= F_SUPPORTED_EVENTLIST;
					pos += SUPPORTED_EVENTLIST_LEN + 1;
					p   += SUPPORTED_EVENTLIST_LEN + 1;
				} else {
					/* skip element */
					for (; pos < len && !IS_DELIM(p); ++pos, ++p);
				}
				break;
		}
	}
	
	return 0;
}