Ejemplo n.º 1
0
static void parse_line(char *ptr, char **varname, char **varval)
{
  /* Skip over any leading spaces */

  ptr = skip_space(ptr);

  /* The first no-space is the beginning of the variable name */

  *varname = skip_space(ptr);
  *varval = NULL;

  /* Parse to the end of the variable name */

  ptr = find_name_end(ptr);

  /* An equal sign is expected next, perhaps after some white space */

  if (*ptr && *ptr != '=')
    {
      /* Some else follows the variable name.  Terminate the variable
       * name and skip over any spaces.
       */

      *ptr = '\0';
       ptr = skip_space(ptr + 1);
    }

  /* Verify that the equal sign is present */

  if (*ptr == '=')
    {
      /* Make sure that the variable name is terminated (this was already
       * done if the name was followed by white space.
       */

      *ptr = '\0';

      /* The variable value should follow =, perhaps separated by some
       * white space.
       */

      ptr = skip_space(ptr + 1);
      if (*ptr)
        {
          /* Yes.. a variable follows.  Save the pointer to the start
           * of the variable string.
           */

          *varval = ptr;

          /* Find the end of the variable string and make sure that it
           * is terminated.
           */

          ptr = find_value_end(ptr);
          *ptr = '\0';
        }
    }
}
Ejemplo n.º 2
0
static void parse_line(char *ptr, char **varname, char **varval)
{
  *varname = ptr;
  *varval = NULL;

   ptr = find_name_end(ptr);
   if (*ptr && *ptr != '=')
    {
      *ptr = '\0';
       ptr = skip_space(ptr + 1);
    }

  if (*ptr == '=')
    {
      *ptr = '\0';
      ptr = skip_space(ptr + 1);
      if (*ptr)
        {
          *varval = ptr;
          ptr = find_value_end(ptr);
          *ptr = '\0';
        }
    }
}
Ejemplo n.º 3
0
/* Converts an attribute string into an attr struct. 
 *
 * Note that the attribute string is trashed.
 *
 * Returns:
 *  SLP_PARAMETER_BAD -- If there is a parse error in the attribute string. 
 *  SLP_OK -- If everything went okay.
 */
SLPError attr_destringify(
		struct xx_SLPAttributes *slp_attr, 
		char *str, 
		SLPInsertionPolicy policy
) {
	char *cur; /* Current index into str. */
	enum {
		/* Note: everything contained in []s in this enum is a production from
		 * RFC 2608's grammar defining attribute lists. 
		 */
		START_ATTR /* The start of an individual [attribute]. */,
		START_TAG /* The start of a [attr-tag]. */,
		VALUE /* The start of an [attr-val]. */,
		STOP_VALUE /* The end of an [attr-val]. */
	} state = START_ATTR; /* The current state of the parse. */
	char *tag; /* A tag that has been parsed. (carries data across state changes)*/
	assert(str != NULL);
	if (strlen(str) == 0) {
		return SLP_OK;
	}
	
	tag = NULL;
	cur = str;
	/***** Pull apart str. *****/
	while (*cur) {
		char *end; /* The end of a parse entity. */
		switch (state) {
			case(START_ATTR): /* At the beginning of an attribute. */
				if (strncmp(cur, VAR_PREFIX, VAR_PREFIX_LEN) == 0) {
					/* At the start of a non-keyword. */
					state = START_TAG;
					cur += VAR_PREFIX_LEN;
				} else {
					/* At the start of a keyword:
					 * Gobble up the keyword and set it. 
					 */
					end = find_tag_end(cur);
					
					if (end == NULL) {
						/* FIXME Ummm, I dunno. */
						assert(0);
					}
					/*** Check that the tag ends on a legal ending char. ***/
					if (*end == ',') {
						/** Add the keyword. **/
						*end = '\0';
						SLPAttrSet_keyw((SLPAttributes)slp_attr, cur);
						cur = end + 1;
						break;
					}
					else if (*end == '\0') {
						SLPAttrSet_keyw((SLPAttributes)slp_attr, cur);
						return SLP_OK; /* FIXME Return success. */
						break;
					} 
					else {
						return SLP_PARAMETER_BAD; /* FIXME Return error code. -- Illegal tag char */
					}
				}
				break;
			case(START_TAG): /* At the beginning of a tag, in brackets. */
				end = find_tag_end(cur);
				
				if (end == NULL) {
					return SLP_PARAMETER_BAD; /* FIXME Err. code -- Illegal char. */
				}
				
				if (*end == '\0') {
					return SLP_PARAMETER_BAD; /* FIXME err: Premature end. */
				}
				
				/*** Check the the end character is valid. ***/
				if (strncmp(end, VAR_INFIX, VAR_INFIX_LEN) == 0) {
					size_t len = end - cur; /* Note that end is on the character _after_ the last character of the tag (the =). */
					assert(tag == NULL);
					tag = (char *)malloc(len + 1); /* TODO This is incorporated into the corresponding attribute. It is therefore free()'d in the var_free(). */
					strncpy(tag, cur, len);
					tag[len] = '\0';
					cur = end + VAR_INFIX_LEN;
					state = VALUE;
				} 
				else {
					/** ERROR! **/
					return SLP_PARAMETER_BAD; /* FIXME Err. code.-- Logic error. */
				}
				
				break;
				
			case(VALUE): /* At the beginning of the value portion. */
				assert(tag != NULL); /* We should not be able to get into this state is the string is malformed. */
				
				/*** Find the end of the value. ***/
				end = find_value_end(cur);
				
				/*** Check the validity of the end chararcter. */
				if ((strncmp(end, VAR_SUFFIX, VAR_SUFFIX_LEN) == 0) 
					|| strncmp(end, VAR_SEPARATOR, VAR_SEPARATOR_LEN) == 0 ) {
			
					SLPAttrStore(slp_attr, tag, cur, end - cur, policy);
					
					cur = end;
					state = STOP_VALUE;
				} else {
					/*** ERROR! ***/
					return SLP_PARAMETER_BAD; /* FIXME err -- invalid value terminator. */
				}
				break;
			case(STOP_VALUE): /* At the end of a value. */
				/***** Check to see which state we should move into next.*****/
				/*** Done? ***/
				if (*cur == '\0') {
					return SLP_OK;
				}
				/*** Another value? (ie, we're in a value list) ***/
				else if (strncmp(cur, VAR_SEPARATOR, VAR_SEPARATOR_LEN)==0) {
					cur += VAR_SEPARATOR_LEN;
					state = VALUE;
				}
				/*** End of the attribute? ***/
				else if (strncmp(cur, VAR_SUFFIX, VAR_SUFFIX_LEN) == 0) {
					assert(tag != NULL);
					free(tag);
					tag = NULL;
					cur += VAR_SUFFIX_LEN;
					
					/*** Are we at string end? ***/
					if (*cur == '\0') {
						return SLP_OK;
					}
					
					/*** Ensure that there is a seperator ***/
					if (strncmp(cur, VAR_SEPARATOR, VAR_SEPARATOR_LEN) != 0) {
						return  SLP_PARAMETER_BAD; /* FIXME err -- unexpected character. */
					}
					
					cur += VAR_SEPARATOR_LEN;
					state = START_ATTR;
				}
				/*** Error. ***/
				else {
					return SLP_PARAMETER_BAD; /* FIXME err -- Illegal char at value end. */
				}
				break;
			default:
				printf("Unknown state %d\n", state);
		}
	}

	return SLP_OK;
}