cvs commit: ALFS/nALFS/src/handlers stage.c

kpfleming at linuxfromscratch.org kpfleming at linuxfromscratch.org
Mon Feb 23 11:44:54 PST 2004


kpfleming    04/02/23 12:44:54

  Modified:    nALFS/src handlers.c handlers.h libXML-tree.c
               nALFS/src/handlers stage.c
  Log:
  implement handler_attribute structure and more powerful attribute parsing
  
  Revision  Changes    Path
  1.22      +0 -11     ALFS/nALFS/src/handlers.c
  
  Index: handlers.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers.c,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- handlers.c	21 Feb 2004 23:09:17 -0000	1.21
  +++ handlers.c	23 Feb 2004 19:44:53 -0000	1.22
  @@ -544,17 +544,6 @@
   	return *string;
   }
   
  -char *parse_string_attribute(const char * const value,
  -			     const char * const message)
  -{
  -	if (strlen(value)) {
  -		return xstrdup(value);
  -	} else {
  -		Nprint_err(message);
  -		return NULL;
  -	}
  -}
  -
   char *parse_string_parameter(const char * const value,
   			     const char * const message)
   {
  
  
  
  1.18      +11 -4     ALFS/nALFS/src/handlers.h
  
  Index: handlers.h
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers.h,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- handlers.h	23 Feb 2004 19:04:21 -0000	1.17
  +++ handlers.h	23 Feb 2004 19:44:53 -0000	1.18
  @@ -51,12 +51,21 @@
   	HDATA_SHELL = (1 << 6),
   } handler_data_e;
   
  +struct handler_attribute {
  +	const char * const name;
  +	const int private;		/* Internal to handler. */
  +	const int content_optional;
  +};
  +
   typedef char *(*handler_data_f)(const element_s * const element,
   				const handler_data_e data_requested);
   typedef int (*handler_f)(element_s * const element);
   typedef int (*handler_test)(element_s * const element, int * const result);
   typedef int (*handler_setup)(element_s * const element);
   typedef void (*handler_free)(const element_s * const element);
  +typedef int (*handler_attribute)(const element_s * const element,
  +				 const struct handler_attribute * const attr,
  +				 const char * const value);
   typedef int (*handler_parse)(const element_s * const element,
   			     const char * const name,
   			     const char * const value);
  @@ -71,7 +80,7 @@
   	const char *description;	/* Short description. */
   	const char *syntax_version;	/* Syntax version string. */
   	const char **parameters;	/* Parameters allowed. */
  -	const char **attributes;	/* Attributes allowed. */
  +	const struct handler_attribute *attributes; /* Attributes allowed. */
   
   	handler_type_e type;
   	handler_data_e data;
  @@ -99,7 +108,7 @@
   	handler_free free;
   	handler_invalid_data invalid_data; /* Validate private data. */
   	handler_invalid_child invalid_child; /* Validate potential child. */
  -	handler_parse parse_attribute;
  +	handler_attribute attribute;
   	handler_parse parse_parameter;
   	handler_parse_content parse_content;
   } handler_info_s;
  @@ -135,8 +144,6 @@
   char *append_param_elements(char **string, element_s *el);
   char *append_prefix_elements(char **string, element_s *el);
   
  -char *parse_string_attribute(const char * const value,
  -			     const char * const message);
   char *parse_string_parameter(const char * const value,
   			     const char * const message);
   char *parse_string_content(const char * const value,
  
  
  
  1.12      +80 -48    ALFS/nALFS/src/libXML-tree.c
  
  Index: libXML-tree.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/libXML-tree.c,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- libXML-tree.c	23 Feb 2004 19:02:11 -0000	1.11
  +++ libXML-tree.c	23 Feb 2004 19:44:53 -0000	1.12
  @@ -113,18 +113,20 @@
   	}
   }
   
  -static const char *find_handler_attribute(const handler_info_s *handler,
  -					  const char *name)
  +static const struct handler_attribute *find_handler_attribute(const handler_info_s *handler,
  +							      const char *name)
   {
   	int i;
  -	const char *attr;
  +	const struct handler_attribute *attr;
   
   	if (!handler->attributes)
   		return NULL;
   
  -	for (i = 0; (attr = handler->attributes[i]); ++i)
  -		if (strcmp(attr, name) == 0)
  +	for (i = 0; (handler->attributes[i].name); ++i) {
  +		attr = &handler->attributes[i];
  +		if (strcmp(attr->name, name) == 0)
   			return attr;
  +	}
   
   	return NULL;
   }
  @@ -145,58 +147,88 @@
   	return NULL;
   }
   
  -static int make_handler_element(xmlNodePtr node, element_s *element)
  +static int parse_node_attributes(xmlNodePtr node, element_s *element)
   {
   	handler_info_s *handler = element->handler;
  -	handler_info_s *parent = element->parent->handler;
  +	xmlAttrPtr attr;
  +	const struct handler_attribute *handler_attr;
  +	const char *content;
   	int result;
   
  -	if (handler->setup && ((result = handler->setup(element)) == 0)) {
  -		xmlAttrPtr attr;
  -		xmlNodePtr child;
  +	/* Pass any attributes present in the node to the
  +	   handler so it can check their values and store
  +	   any data associated with them.
  +	*/
  +
  +	for (attr = node->properties; attr; attr = attr->next) {
  +		handler_attr = find_handler_attribute(handler,
  +						      (const char *) attr->name);
  +		if (!handler_attr) {
  +			Nprint_warn("<%s>: \"%s\" attribute is not supported.", handler->name, (const char *) attr->name);
  +			continue;
  +		}
  +		
  +		content = attr->children->content;
  +
  +		if ((!handler_attr->content_optional) && (strlen(content) == 0)) {
  +			Nprint_err("<%s>: \"%s\" attribute cannot be empty.", handler->name, handler_attr->name);
  +			return 1;
  +		}
  +
  +		result = handler->attribute(element, handler_attr, content);
  +		if (result)
  +			return result;
  +	}
  +
  +	return 0;
  +}
  +
  +static int parse_node_parameters(xmlNodePtr node, element_s *element)
  +{
  +	handler_info_s *handler = element->handler;
  +	xmlNodePtr child;
  +	int result;
   
  -		/* Pass any attributes present in the node to the
  -		   handler so it can check their values and store
  -		   any data associated with them.
  -		*/
  -
  -		for (attr = node->properties; attr; attr = attr->next) {
  -			if (!find_handler_attribute(handler,
  -						    (const char *) attr->name)) {
  -				Nprint_warn("<%s>: \"%s\" attribute is not supported.", handler->name, (const char *) attr->name);
  -				continue;
  -			}
  -
  -			result = handler->parse_attribute(element,
  -							  (const char *) attr->name,
  -							  (const char *) attr->children->content);
  +	/* Check all elements inside the node to see if they are
  +	   either acceptable parameters for the handler or
  +	   elements with known handlers.
  +	*/
  +
  +	for (child = node->children; child; child = child->next) {
  +		if (!USED_NODE(child))
  +			continue;
  +
  +		if (find_handler_parameter(handler,
  +					   (const char *) child->name)) {
  +			result = handler->parse_parameter(element,
  +							  (const char *) child->name,
  +							  (const char *) child->children->content);
   			if (result)
   				return result;
  +		} else if (!find_handler(child->name, syntax_version)) {
  +			Nprint_warn("<%s>: <%s> parameter is not supported.", 
  +				    handler->name,
  +				    (const char *) child->name);
   		}
  +	}
   
  -		/* Check all elements inside the node to see if they are
  -		   either acceptable parameters for the handler or
  -		   elements with known handlers.
  -		*/
  -
  -		for (child = node->children; child; child = child->next)
  -		{
  -			if (!USED_NODE(child))
  -				continue;
  -
  -			if (find_handler_parameter(handler,
  -						   (const char *) child->name)) {
  -				result = handler->parse_parameter(element,
  -								  (const char *) child->name,
  -								  (const char *) child->children->content);
  -				if (result)
  -					return result;
  -			} else if (!find_handler(child->name, syntax_version)) {
  -				Nprint_warn("<%s>: <%s> parameter is not supported.", 
  -					   handler->name,
  -					   (const char *) child->name);
  -			}
  -		}
  +	return 0;
  +}
  +
  +static int make_handler_element(xmlNodePtr node, element_s *element)
  +{
  +	handler_info_s *handler = element->handler;
  +	handler_info_s *parent = element->parent->handler;
  +	int result;
  +
  +	if (handler->setup && ((result = handler->setup(element)) == 0)) {
  +		result = parse_node_attributes(node, element);
  +		if (result)
  +			return result;
  +
  +		result = parse_node_parameters(node, element);
  +		if (result)
  +			return result;
   
   		/* If the handler cares about its content, pass it in
   		   and check the result. */
  
  
  
  1.24      +47 -29    ALFS/nALFS/src/handlers/stage.c
  
  Index: stage.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/stage.c,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- stage.c	23 Feb 2004 19:02:11 -0000	1.23
  +++ stage.c	23 Feb 2004 19:44:54 -0000	1.24
  @@ -216,7 +216,14 @@
   
   /* <stage> handler */
   
  -static const char *stage_attributes[] = { "name", NULL};
  +enum stage_attribute_types {
  +	STAGE_NAME,
  +};
  +
  +static struct handler_attribute stage_attributes[] = {
  +	{ .name = "name", .private = STAGE_NAME },
  +	{ .name = NULL }
  +};
   
   struct stage_data {
   	char *name;
  @@ -309,17 +316,18 @@
   }
   
   static int stage_attribute(const element_s * const element,
  -			   const char * const attribute,
  +			   const struct handler_attribute * const attr,
   			   const char * const value)
   {
   	struct stage_data *data = (struct stage_data *) element->handler_data;
   
  -	if (strcmp(attribute, "name") == 0)
  -		if ((data->name = parse_string_attribute(value,
  -							 "<stage>: \"name\" cannot be empty.")))
  -			return 0;
  -
  -	return 1;
  +	switch (attr->private) {
  +	case STAGE_NAME:
  +		data->name = xstrdup(value);
  +		return 0;
  +	default:
  +		return 1;
  +	}
   }
   
   static int stage_invalid_child(const element_s * const element,
  @@ -590,7 +598,16 @@
   
   /* <variable> handler */
   
  -static const char *variable_attributes[] = { "mode", "name", NULL};
  +enum variable_attribute_types {
  +	VARIABLE_NAME,
  +	VARIABLE_MODE,
  +};
  +
  +static struct handler_attribute variable_attributes[] = {
  +	{ .name = "name", .private = VARIABLE_NAME },
  +	{ .name = "mode", .private = VARIABLE_MODE },
  +	{ .name = NULL }
  +};
   
   enum variable_mode {
   	VAR_SET,
  @@ -629,28 +646,29 @@
   }
   
   static int variable_attribute(const element_s * const element,
  -			      const char * const attribute,
  +			      const struct handler_attribute * const attr,
   			      const char * const value)
   {
   	struct variable_data *data = (struct variable_data *) element->handler_data;
   
  -	if (strcmp(attribute, "name") == 0)
  -		if ((data->name = parse_string_attribute(value,
  -							 "<variable>: \"name\" cannot be empty.")))
  -			return 0;
  -
  -	if (strcmp(attribute, "mode") == 0) {
  +	switch (attr->private) {
  +	case VARIABLE_NAME:
  +		data->name = xstrdup(value);
  +		return 0;
  +	case VARIABLE_MODE:
   		if (strcmp(value, "append") == 0) {
   			data->mode = VAR_APPEND;
   			return 0;
   		} else if (strcmp(value, "prepend") == 0) {
   			data->mode = VAR_PREPEND;
   			return 0;
  -		} else
  +		} else {
   			Nprint_err("<variable>: unknown \"mode\" (%s)", value);
  +			return 1;
  +		}
  +	default:
  +		return 1;
   	}
  -
  -	return 1;
   }
   
   static int variable_content(const element_s * const element,
  @@ -728,7 +746,7 @@
   		.setup = stage_setup,
   		.free = stage_free,
   		.attributes = stage_attributes,
  -		.parse_attribute = stage_attribute,
  +		.attribute = stage_attribute,
   		.invalid_child = stage_invalid_child,
   	},
   	{
  @@ -761,7 +779,7 @@
   		.setup = variable_setup,
   		.free = variable_free,
   		.attributes = variable_attributes,
  -		.parse_attribute = variable_attribute,
  +		.attribute = variable_attribute,
   		.parse_content = variable_content,
   		.invalid_data = variable_invalid_data,
   		.invalid_child = variable_invalid_child,
  @@ -779,7 +797,7 @@
   		.setup = stage_setup,
   		.free = stage_free,
   		.attributes = stage_attributes,
  -		.parse_attribute = stage_attribute,
  +		.attribute = stage_attribute,
   		.invalid_child = stage_invalid_child,
   	},
   	{
  @@ -793,7 +811,7 @@
   		.setup = stage_setup,
   		.free = stage_free,
   		.attributes = stage_attributes,
  -		.parse_attribute = stage_attribute,
  +		.attribute = stage_attribute,
   		.invalid_child = stage_invalid_child,
   	},
   	{
  @@ -807,7 +825,7 @@
   		.setup = stage_setup,
   		.free = stage_free,
   		.attributes = stage_attributes,
  -		.parse_attribute = stage_attribute,
  +		.attribute = stage_attribute,
   		.invalid_child = stage_invalid_child,
   	},
   	{
  @@ -840,7 +858,7 @@
   		.setup = variable_setup,
   		.free = variable_free,
   		.attributes = variable_attributes,
  -		.parse_attribute = variable_attribute,
  +		.attribute = variable_attribute,
   		.parse_content = variable_content,
   		.invalid_data = variable_invalid_data,
   		.invalid_child = variable_invalid_child,
  @@ -858,7 +876,7 @@
   		.setup = stage_setup,
   		.free = stage_free,
   		.attributes = stage_attributes,
  -		.parse_attribute = stage_attribute,
  +		.attribute = stage_attribute,
   		.invalid_child = stage_invalid_child,
   	},
   	{
  @@ -872,7 +890,7 @@
   		.setup = stage_setup,
   		.free = stage_free,
   		.attributes = stage_attributes,
  -		.parse_attribute = stage_attribute,
  +		.attribute = stage_attribute,
   		.invalid_child = stage_invalid_child,
   	},
   	{
  @@ -886,7 +904,7 @@
   		.setup = stage_setup,
   		.free = stage_free,
   		.attributes = stage_attributes,
  -		.parse_attribute = stage_attribute,
  +		.attribute = stage_attribute,
   		.invalid_child = stage_invalid_child,
   	},
   	{
  @@ -919,7 +937,7 @@
   		.setup = variable_setup,
   		.free = variable_free,
   		.attributes = variable_attributes,
  -		.parse_attribute = variable_attribute,
  +		.attribute = variable_attribute,
   		.parse_content = variable_content,
   		.invalid_data = variable_invalid_data,
   		.invalid_child = variable_invalid_child,
  
  
  



More information about the alfs-log mailing list