cvs commit: ALFS/nALFS/src/handlers execute.c patch.c

kpfleming at linuxfromscratch.org kpfleming at linuxfromscratch.org
Tue Mar 2 18:12:30 PST 2004


kpfleming    04/03/02 19:12:30

  Modified:    nALFS/src/handlers execute.c patch.c
  Log:
  convert to new parsing system
  
  Revision  Changes    Path
  1.26      +219 -123  ALFS/nALFS/src/handlers/execute.c
  
  Index: execute.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/execute.c,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- execute.c	29 Feb 2004 04:24:59 -0000	1.25
  +++ execute.c	3 Mar 2004 02:12:30 -0000	1.26
  @@ -44,178 +44,260 @@
   #include "backend.h"
   #include "options.h"
   
  -
  -#if HANDLER_SYNTAX_2_0
  -
  -static const struct handler_parameter execute_parameters_v2[] = {
  -	{ .name = "base" },
  -	{ .name = "command" },
  -	{ .name = "param" },
  -	{ .name = NULL }
  +enum {
  +	EXECUTE_BASE,
  +	EXECUTE_COMMAND,
  +	EXECUTE_PARAM,
  +	EXECUTE_PREFIX,
  +	EXECUTE_CONTENT,
   };
   
  -static int execute_main_ver2(const element_s * const el)
  -{
  -	int status;
  +struct execute_data {
   	char *base;
   	char *command;
  +	char *param;
  +	int param_seen;
  +	char *prefix;
  +	int prefix_seen;
  +	char *content;
  +};
   
  +static int execute_setup(element_s * const element)
  +{
  +	struct execute_data *data;
   
  -	if ((command = alloc_trimmed_param_value("command", el)) == NULL) {
  -		Nprint_h_err("No command specified.");
  +	if ((data = xmalloc(sizeof(struct execute_data))) == NULL)
   		return -1;
  -	}
   
  -	base = alloc_base_dir(el);
  +	data->command = NULL;
  +	data->param = xstrdup(" ");
  +	data->param_seen = 0;
  +	data->prefix = xstrdup("");
  +	data->prefix_seen = 0;
  +	data->base = NULL;
  +	data->content = NULL;
  +	element->handler_data = data;
   
  -	if (change_current_dir(base)) {
  -		xfree(base);
  -		xfree(command);
  -		return -1;
  -	}
  +	return 0;
  +}
   
  -	append_param_elements(&command, el);
  +static void execute_free(const element_s * const element)
  +{
  +	struct execute_data *data = (struct execute_data *) element->handler_data;
   
  -	Nprint_h("Executing system command in %s:", base);
  -	Nprint_h("    %s", command);
  +	xfree(data->base);
  +	xfree(data->command);
  +	xfree(data->prefix);
  +	xfree(data->param);
  +	xfree(data->content);
  +	xfree(data);
  +}
   
  -	status = execute_command(el, "%s", command);
  +static int execute_attribute(const element_s * const element,
  +			     const struct handler_attribute * const attr,
  +			     const char * const value)
  +{
  +	struct execute_data *data = (struct execute_data *) element->handler_data;
   
  -	xfree(base);
  -	xfree(command);
  +	switch (attr->private) {
  +	case EXECUTE_BASE:
  +		if (data->base) {
  +			Nprint_err("<%s>: cannot specify \"base\" more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->base = xstrdup(value);
  +		return 0;
  +	case EXECUTE_COMMAND:
  +		if (data->command) {
  +			Nprint_err("<%s>: cannot specify \"command\" more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->command = xstrdup(value);
  +		return 0;
  +	default:
  +		return 1;
  +	}
  +}
   
  -	return status;
  -	
  +static int execute_parameter(const element_s * const element,
  +			     const struct handler_parameter * const param,
  +			     const char * const value)
  +{
  +	struct execute_data *data = (struct execute_data *) element->handler_data;
  +
  +	switch (param->private) {
  +	case EXECUTE_BASE:
  +		if (data->base) {
  +			Nprint_err("<%s>: cannot specify <base> more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->base = xstrdup(value);
  +		return 0;
  +	case EXECUTE_COMMAND:
  +		if (data->command) {
  +			Nprint_err("<%s>: cannot specify <command> more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->command = xstrdup(value);
  +		return 0;
  +	case EXECUTE_CONTENT:
  +		if (data->content) {
  +			Nprint_err("<%s>: cannot specify <content> more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->content = xstrdup(value);
  +		return 0;
  +	case EXECUTE_PREFIX:
  +		append_str(&data->prefix, value);
  +		append_str(&data->prefix, " ");
  +		data->prefix_seen = 1;
  +		return 0;
  +	case EXECUTE_PARAM:
  +		append_str(&data->param, value);
  +		append_str(&data->param, " ");
  +		data->param_seen = 1;
  +		return 0;
  +	default:
  +		return 1;
  +	}
   }
   
  -static char *execute_data_ver2(const element_s * const el,
  -			       const handler_data_e data)
  +#if HANDLER_SYNTAX_2_0
  +
  +static const struct handler_parameter execute_parameters_v2[] = {
  +	{ .name = "base", .private = EXECUTE_BASE },
  +	{ .name = "command", .private = EXECUTE_COMMAND },
  +	{ .name = "param", .private = EXECUTE_PARAM },
  +	{ .name = NULL }
  +};
  +
  +static int execute_valid_data_v2(const element_s * const element)
   {
  -	(void) data;
  +	struct execute_data *data = (struct execute_data *) element->handler_data;
  +
  +	if (!data->command) {
  +		Nprint_err("<%s>: <command> must be specified.", element->handler->name);
  +		return 0;
  +	}
   
  -	return alloc_trimmed_param_value("command", el);
  +	return 1;
   }
   
   #endif /* HANDLER_SYNTAX_2_0 */
   
  -
   #if HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1
   
   static const struct handler_parameter execute_parameters_v3[] = {
  -	{ .name = "prefix" },
  -	{ .name = "param" },
  +	{ .name = "prefix", .private = EXECUTE_PREFIX },
  +	{ .name = "param", .private = EXECUTE_PARAM },
   	{ .name = NULL }
   };
   
   static const struct handler_attribute execute_attributes_v3[] = {
  -	{ .name = "command" },
  -	{ .name = "base" },
  +	{ .name = "command", .private = EXECUTE_COMMAND },
  +	{ .name = "base", .private = EXECUTE_BASE },
   	{ .name = NULL }
   };
   
  -static int execute_main_ver3(const element_s * const el)
  +static int execute_valid_data_v3(const element_s * const element)
   {
  -	int status;
  -	char *c, *command;
  -
  +	struct execute_data *data = (struct execute_data *) element->handler_data;
   
  -	if (change_to_base_dir(el, attr_value("base", el), 1))
  -		return -1;
  -
  -	if ((c = attr_value("command", el)) == NULL) {
  -		Nprint_h_err("No command specified.");
  -		return -1;
  +	if (!data->command) {
  +		Nprint_err("<%s>: \"command\" must be specified.", element->handler->name);
  +		return 0;
   	}
   
  -	command = xstrdup("");
  -
  -	append_prefix_elements(&command, el);
  +	return 1;
  +}
   
  -	append_str(&command, c);
  +#endif /* HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 */
   
  -	append_param_elements(&command, el);
  +#if HANDLER_SYNTAX_2_0 || HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1
   
  -	Nprint_h("Executing system command:");
  -	Nprint_h("    %s", command);
  +static int execute_main(const element_s * const element)
  +{
  +	struct execute_data *data = (struct execute_data *) element->handler_data;
  +	int status;
   
  -	status = execute_command(el, "%s", command);
  +	if (change_to_base_dir(element, data->base, 1))
  +		return -1;
  +	
  +	Nprint_h("Executing system command");
  +	Nprint_h("    %s%s%s", data->prefix, data->command, data->param);
   
  -	xfree(command);
  +	status = execute_command(element, "%s%s%s", data->prefix, data->command, data->param);
   
   	return status;
   }
   
  -static char *execute_data_ver3(const element_s * const el,
  -			       const handler_data_e data)
  +static char *execute_data(const element_s * const element,
  +			  const handler_data_e data_requested)
   {
  -	char *command;
  +	struct execute_data *data = (struct execute_data *) element->handler_data;
   
  -	(void) data;
  -
  -	if ((command = attr_value("command", el))) {
  -		return xstrdup(command);
  -	}
  +	(void) data_requested;
   
  -	return NULL;
  +	return xstrdup(data->command);
   }
   
  -#endif /* HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 */
  -
  +#endif /* HANDLER_SYNTAX_2_0 || HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 */
   
   #ifdef HANDLER_SYNTAX_3_2
   
   static const struct handler_parameter execute_parameters_v3_2[] = {
  -	{ .name = "prefix" },
  -	{ .name = "param" },
  -	{ .name = "content" },
  +	{ .name = "prefix", .private = EXECUTE_PREFIX },
  +	{ .name = "param", .private = EXECUTE_PARAM },
  +	{ .name = "content", .private = EXECUTE_CONTENT },
   	{ .name = NULL }
   };
   
   static const struct handler_attribute execute_attributes_v3_2[] = {
  -	{ .name = "command" },
  -	{ .name = "base" },
  +	{ .name = "command", .private = EXECUTE_COMMAND },
  +	{ .name = "base", .private = EXECUTE_BASE },
   	{ .name = NULL }
   };
   
  -static int execute_main_ver3_2(const element_s * const el)
  +static int execute_valid_data_v3_2(const element_s * const element)
   {
  -	int status = -1;
  -	char *c;
  -	char *content = NULL;
  +	struct execute_data *data = (struct execute_data *) element->handler_data;
   
  -	if (change_to_base_dir(el, attr_value("base", el), 1))
  -		return -1;
  -
  -	c = attr_value("command", el);
  -	content = alloc_trimmed_param_value("content", el);
  +	if (!(data->command || data->content)) {
  +		Nprint_err("<%s>: either \"command\" or <content> must be specified.", element->handler->name);
  +		return 0;
  +	}
   
  -	if (!(c || content)) {
  -		Nprint_h_err("Either command or <content> must be specified.");
  -		return -1;
  +	if (data->command && data->content) {
  +		Nprint_err("<%s>: cannot specify both \"command\" and <content>.", element->handler->name);
  +		return 0;
   	}
  -	
  -	if (c && content) {
  -		Nprint_h_err("Cannot specify both command and <content>.");
  -		return -1;
  +
  +	if (data->content && data->param_seen) {
  +		Nprint_err("<%s>: cannot specify both <content> and <param>.", element->handler->name);
  +		return 0;
   	}
   
  -	if (content &&
  -	    (first_param("prefix", el) || first_param("param", el))) {
  -		Nprint_h_err("Cannot specify both <content> and <param>/<prefix>.");
  -		return -1;
  +	if (data->content && data->prefix_seen) {
  +		Nprint_err("<%s>: cannot specify both <content> and <prefix>.", element->handler->name);
  +		return 0;
   	}
   
  -	if (c) {
  -		char *command;
  +	return 1;
  +}
  +
  +static int execute_main_ver3_2(const element_s * const element)
  +{
  +	struct execute_data *data = (struct execute_data *) element->handler_data;
  +	int status = -1;
   
  -		command = xstrdup("");
  -		append_prefix_elements(&command, el);
  -		append_str(&command, c);
  -		append_param_elements(&command, el);
  +	if (change_to_base_dir(element, data->base, 1))
  +		return -1;
  +	
  +	if (data->command) {
   		Nprint_h("Executing system command:");
  -		Nprint_h("    %s", command);
  -		status = execute_command(el, "%s", command);
  -		xfree(command);
  +		Nprint_h("    %s%s%s", data->prefix, data->command, data->param);
  +		status = execute_command(element, "%s%s%s", data->prefix, data->command,
  +					 data->param);
   	} else {
   		FILE *temp_script;
   		char *tok;
  @@ -227,12 +309,12 @@
   		append_str(&temp_file_name, ".nALFS.XXXXXX");
   		if (!create_temp_file(temp_file_name)) {
   			if ((temp_script = fopen(temp_file_name, "w"))) {
  -				for (tok = strtok(content, "\n");
  -				     tok;
  -				     tok = strtok(NULL, "\n")) {
  +				char *tmp = xstrdup(data->content);
  +				for (tok = strtok(tmp, "\n"); tok; tok = strtok(NULL, "\n")) {
   					fprintf(temp_script, "%s\n", ++tok);
   				}
   				fclose(temp_script);
  +				xfree(tmp);
   				if (chmod(temp_file_name, S_IRUSR|S_IXUSR)) {
   					Nprint_h_err("Cannot make temporary script executable.");
   					Nprint_h_err("    %s (%s)", temp_file_name, strerror(errno));
  @@ -250,31 +332,26 @@
   		xfree(temp_file_name);
   	}
   
  -	xfree(content);
  -
   	return status;
   }
   
  -static char *execute_data_ver3_2(const element_s * const el,
  -				 const handler_data_e data)
  +static char *execute_data_ver3_2(const element_s * const element,
  +				 const handler_data_e data_requested)
   {
  -	char *command;
  -	element_s *content_param;
  +	struct execute_data *data = (struct execute_data *) element->handler_data;
   
  -	(void) data;
  +	(void) data_requested;
   
  -	if ((command = attr_value("command", el))) {
  -		return xstrdup(command);
  -	} else if ((content_param = first_param("content", el))) {
  -		return alloc_trimmed_str(content_param->content);
  +	if (data->command) {
  +		return xstrdup(data->command);
  +	} else if (data->content) {
  +		return xstrdup(data->content);
   	}
  -
   	return NULL;
   }
   
   #endif
   
  -
   /*
    * Handlers' information.
    */
  @@ -286,10 +363,14 @@
   		.description = "Execute",
   		.syntax_version = "2.0",
   		.parameters = execute_parameters_v2,
  -		.main = execute_main_ver2,
  +		.main = execute_main,
   		.type = HTYPE_EXECUTE,
  -		.alloc_data = execute_data_ver2,
  +		.alloc_data = execute_data,
   		.is_action = 1,
  +		.setup = execute_setup,
  +		.free = execute_setup,
  +		.parameter = execute_parameter,
  +		.valid_data = execute_valid_data_v2,
   	},
   #endif
   #if HANDLER_SYNTAX_3_0
  @@ -299,10 +380,15 @@
   		.syntax_version = "3.0",
   		.parameters = execute_parameters_v3,
   		.attributes = execute_attributes_v3,
  -		.main = execute_main_ver3,
  +		.main = execute_main,
   		.type = HTYPE_EXECUTE,
  -		.alloc_data = execute_data_ver3,
  +		.alloc_data = execute_data,
   		.is_action = 1,
  +		.setup = execute_setup,
  +		.free = execute_free,
  +		.parameter = execute_parameter,
  +		.attribute = execute_attribute,
  +		.valid_data = execute_valid_data_v3,
   	},
   #endif
   #if HANDLER_SYNTAX_3_1
  @@ -312,10 +398,15 @@
   		.syntax_version = "3.1",
   		.parameters = execute_parameters_v3,
   		.attributes = execute_attributes_v3,
  -		.main = execute_main_ver3,
  +		.main = execute_main,
   		.type = HTYPE_EXECUTE,
  -		.alloc_data = execute_data_ver3,
  +		.alloc_data = execute_data,
   		.is_action = 1,
  +		.setup = execute_setup,
  +		.free = execute_free,
  +		.parameter = execute_parameter,
  +		.attribute = execute_attribute,
  +		.valid_data = execute_valid_data_v3,
   	},
   #endif
   #if HANDLER_SYNTAX_3_2
  @@ -330,6 +421,11 @@
   		.alloc_data = execute_data_ver3_2,
   		.is_action = 1,
   		.alternate_shell = 1,
  +		.setup = execute_setup,
  +		.free = execute_free,
  +		.parameter = execute_parameter,
  +		.attribute = execute_attribute,
  +		.valid_data = execute_valid_data_v3_2,
   	},
   #endif
   	{
  
  
  
  1.24      +268 -145  ALFS/nALFS/src/handlers/patch.c
  
  Index: patch.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/patch.c,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- patch.c	29 Feb 2004 04:24:59 -0000	1.23
  +++ patch.c	3 Mar 2004 02:12:30 -0000	1.24
  @@ -43,43 +43,178 @@
   #include "backend.h"
   
   
  -#if HANDLER_SYNTAX_2_0
  -
  -static const struct handler_parameter patch_parameters_v2[] = {
  -	{ .name = "base" },
  -	{ .name = "param" },
  -	{ .name = NULL }
  +enum {
  +	PATCH_BASE,
  +	PATCH_PREFIX,
  +	PATCH_PARAM,
  +	PATCH_REFERENCE,
  +	PATCH_FILE,
  +	PATCH_MODE,
  +	PATCH_PATH_STRIP,
   };
   
  -static int patch_main_ver2(const element_s * const el)
  -{
  -	int status;
  +struct patch_data {
   	char *base;
  -	char *parameters = NULL;
  +	char *prefix;
  +	char *param;
  +	int param_seen;
  +	int reference_count;
  +	char **references;
  +	char *file;
  +	const element_s *digest;
  +	int forward_mode;
  +	int path_strip;
  +};
   
  +static int patch_setup(element_s * const element)
  +{
  +	struct patch_data *data;
   
  -	if (append_param_elements(&parameters, el) == NULL) {
  -		Nprint_h_err("No patch parameters specified.");
  +	if ((data = xmalloc(sizeof(struct patch_data))) == NULL)
   		return -1;
  +
  +	data->file = NULL;
  +	data->param = xstrdup(" ");
  +	data->param_seen = 0;
  +	data->prefix = xstrdup("");
  +	data->reference_count = 0;
  +	data->references = NULL;
  +	data->digest = NULL;
  +	data->base = NULL;
  +	data->path_strip = 1;
  +	data->forward_mode = 1;
  +	element->handler_data = data;
  +
  +	return 0;
  +}
  +
  +static void patch_free(const element_s * const element)
  +{
  +	struct patch_data *data = (struct patch_data *) element->handler_data;
  +	int i;
  +
  +	xfree(data->base);
  +	xfree(data->file);
  +	xfree(data->prefix);
  +	xfree(data->param);
  +	if (data->reference_count > 0) {
  +		for (i = 0; i < data->reference_count; i++)
  +			xfree(data->references[i]);
  +		xfree(data->references);
   	}
  +	xfree(data);
  +}
   
  -	base = alloc_base_dir(el);
  +static int patch_attribute(const element_s * const element,
  +			   const struct handler_attribute * const attr,
  +			   const char * const value)
  +{
  +	struct patch_data *data = (struct patch_data *) element->handler_data;
   
  -	if (change_current_dir(base)) {
  -		xfree(base);
  -		xfree(parameters);
  -		return -1;
  +	switch (attr->private) {
  +	case PATCH_BASE:
  +		if (data->base) {
  +			Nprint_err("<%s>: cannot specify \"base\" more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->base = xstrdup(value);
  +		return 0;
  +	case PATCH_MODE:
  +		if (!strcmp(value, "forward")) {
  +			data->forward_mode = 1;
  +			return 0;
  +		} else if (!strcmp(value, "reverse")) {
  +			data->forward_mode = 0;
  +			return 0;
  +		} else {
  +			Nprint_err("<%s>: \"mode\" must be \"forward\" or \"reverse\".", element->handler->name);
  +			return 1;
  +		}
  +	case PATCH_PATH_STRIP:
  +		data->path_strip = atoi(value);
  +		return 0;
  +	default:
  +		return 1;
   	}
  -	
  -	Nprint_h("Patching in %s", base);
  -	Nprint_h("    patch %s", parameters);
  +}
   
  -	if ((status = execute_command(el, "patch %s", parameters))) {
  -		Nprint_h_err("Patching failed.");
  +static int patch_parameter(const element_s * const element,
  +			   const struct handler_parameter * const param,
  +			   const char * const value)
  +{
  +	struct patch_data *data = (struct patch_data *) element->handler_data;
  +
  +	switch (param->private) {
  +	case PATCH_BASE:
  +		if (data->base) {
  +			Nprint_err("<%s>: cannot specify \"base\" more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->base = xstrdup(value);
  +		return 0;
  +	case PATCH_PREFIX:
  +		append_str(&data->prefix, value);
  +		append_str(&data->prefix, " ");
  +		return 0;
  +	case PATCH_PARAM:
  +		append_str(&data->param, value);
  +		append_str(&data->param, " ");
  +		data->param_seen = 1;
  +		return 0;
  +	case PATCH_FILE:
  +		if (data->file) {
  +			Nprint_err("<%s>: cannot specify <file> more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->file = xstrdup(value);
  +		return 0;
  +	case PATCH_REFERENCE:
  +		data->reference_count++;
  +		if ((data->references = xrealloc(data->references,
  +						 sizeof(data->references[0]) * (data->reference_count))) == NULL) {
  +			Nprint_err("xrealloc() failed: %s", strerror(errno));
  +			return -1;
  +		}
  +		data->references[(data->reference_count - 1)] = xstrdup(value);
  +		return 0;
  +	default:
  +		return 1;
  +	}
  +}
  +
  +static int patch_valid_data(const element_s * const element)
  +{
  +	struct patch_data *data = (struct patch_data *) element->handler_data;
  +
  +	if (!data->param_seen) {
  +		Nprint_err("<%s>: at least one <param> must be specified.", element->handler->name);
  +		return 0;
   	}
  +
  +	return 1;
  +}
  +
  +#if HANDLER_SYNTAX_2_0
  +
  +static const struct handler_parameter patch_parameters_v2[] = {
  +	{ .name = "base", .private = PATCH_BASE },
  +	{ .name = "param", .private = PATCH_PARAM },
  +	{ .name = NULL }
  +};
  +
  +static int patch_main_ver2(const element_s * const element)
  +{
  +	struct patch_data *data = (struct patch_data *) element->handler_data;
  +	int status;
  +
  +	if (change_to_base_dir(element, data->base, 1))
  +		return -1;
   	
  -	xfree(base);
  -	xfree(parameters);
  +	Nprint_h("Patching:");
  +	Nprint_h("    patch%s", data->param);
  +
  +	if ((status = execute_command(element, "patch%s", data->param)))
  +		Nprint_h_err("Patching failed.");
   
   	return status;
   }
  @@ -90,188 +225,156 @@
   #if HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1
   
   static const struct handler_parameter patch_parameters_v3[] = {
  -	{ .name = "prefix" },
  -	{ .name = "param" },
  +	{ .name = "prefix", .private = PATCH_PREFIX },
  +	{ .name = "param", .private = PATCH_PARAM },
   	{ .name = NULL }
   };
   
   static const struct handler_attribute patch_attributes_v3[] = {
  -	{ .name = "base" },
  +	{ .name = "base", .private = PATCH_BASE },
   	{ .name = NULL }
   };
   
  -static int patch_main_ver3(const element_s * const el)
  +static int patch_main_ver3(const element_s * const element)
   {
  +	struct patch_data *data = (struct patch_data *) element->handler_data;
   	int status;
  -	char *parameters = NULL;
  -	char *command;
  -
  -
  -	if (change_to_base_dir(el, attr_value("base", el), 1))
  -		return -1;
  +	char *command = NULL;
   
  -	if (append_param_elements(&parameters, el) == NULL) {
  -		Nprint_h_err("No patch parameters specified.");
  +	if (change_to_base_dir(element, data->base, 1))
   		return -1;
  -	}
  +	
  +	append_str(&command, data->prefix);
  +	append_str(&command, "patch");
  +	append_str(&command, data->param);
   
   	Nprint_h("Patching");
  -	command = xstrdup("");
  -
  -	append_prefix_elements(&command, el);
  -
  -	/* the trailing space on the string below is important, since
  -	   append_param_elements will not have put a space at the
  -	   beginning of the parameters string (since it was NULL to
  -	   begin with)
  -	*/
  -
  -	append_str(&command, "patch ");
  -
  -	append_str(&command, parameters);
  -
   	Nprint_h("    %s", command);
   
  -	if ((status = execute_command(el, command))) {
  +	if ((status = execute_command(element, command)))
   		Nprint_h_err("Patching failed.");
  -	}
   	
   	xfree(command);
  -	xfree(parameters);
   
   	return status;
   }
   
   #endif /* HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 */
   
  -
   #if HANDLER_SYNTAX_3_2
   
   static const struct handler_parameter patch_parameters_v3_2[] = {
  -	{ .name = "prefix" },
  -	{ .name = "param" },
  -	{ .name = "digest" },
  -	{ .name = "reference" },
  -	{ .name = "file" },
  +	{ .name = "prefix", .private = PATCH_PREFIX },
  +	{ .name = "param", .private = PATCH_PARAM },
  +	{ .name = "reference", .private = PATCH_REFERENCE },
  +	{ .name = "file", .private = PATCH_FILE },
   	{ .name = NULL }
   };
   
   static const struct handler_attribute patch_attributes_v3_2[] = {
  -	{ .name = "base" },
  -	{ .name = "mode" },
  -	{ .name = "path_strip" },
  +	{ .name = "base", .private = PATCH_BASE },
  +	{ .name = "mode", .private = PATCH_MODE },
  +	{ .name = "path_strip", .private = PATCH_PATH_STRIP },
   	{ .name = NULL }
   };
   
  -static int patch_main_ver3_2(const element_s * const el)
  +static int patch_valid_data_v3_2(const element_s * const element)
   {
  +	struct patch_data *data = (struct patch_data *) element->handler_data;
  +
  +	if (!data->file) {
  +		Nprint_err("<%s>: <file> must be specified.", element->handler->name);
  +		return 0;
  +	}
  +
  +	return 1;
  +}
  +
  +static int patch_valid_child_v3_2(const element_s * const element,
  +				  const element_s * const child)
  +{
  +	struct patch_data *data = (struct patch_data *) element->handler_data;
  +
  +	if (child->handler->type & HTYPE_DIGEST) {
  +		if (data->digest) {
  +			Nprint_err("<%s>: only one <digest> allowed.", element->handler->name);
  +			return 0;
  +		}
  +
  +		data->digest = child;
  +		return 1;
  +	}
  +
  +	return 0;
  +}
  +
  +static int patch_main_ver3_2(const element_s * const element)
  +{
  +	struct patch_data *data = (struct patch_data *) element->handler_data;
           int status = -1;
  -	char *file = NULL;
  -	char *command = NULL;
   	char *digest = NULL;
   	char *digest_type = NULL;
   	char *decompressor = NULL;
  -	char *mode;
  -	char *path_strip;
   	struct stat file_stat;
   
  -
  -	if (change_to_base_dir(el, attr_value("base", el), 1))
  +	if (change_to_base_dir(element, data->base, 1))
   		return -1;
   
  -	if ((file = alloc_trimmed_param_value("file", el)) == NULL) {
  -		Nprint_h_err("File name is missing.");
  -		goto free_all_and_return;
  +	if (data->digest) {
  +		digest = data->digest->handler->alloc_data(data->digest, HDATA_COMMAND);
  +		digest_type = data->digest->handler->alloc_data(data->digest, HDATA_VERSION);
   	}
   
  -	alloc_element_digest(el, &digest, &digest_type);
  -
  -	/* Check if file exists and if not attempt to download it. */
  -	if ((stat(file, &file_stat))) {
  -		if (errno == ENOENT && first_param("reference", el) != NULL) {
  -			int found = 0;
  -			element_s *p;
  +	/* Check if file exists. */
  +	if ((stat(data->file, &file_stat))) {
  +	        if ((errno == ENOENT) && (data->reference_count > 0)) {
  +			int i;
   
  -			Nprint_h_warn("File %s not found.", file);
  +			Nprint_h_warn("File %s not found.", data->file);
   			Nprint_h("Trying to fetch it from <reference>...");
   
  -			for (p = first_param("reference", el); p; p = next_param(p)) {
  -				char *s;
  -
  -				if ((s = alloc_trimmed_str(p->content)) == NULL) {
  -					Nprint_h_warn("Source empty.");
  -					continue;
  -				}
  -
  -				if (! get_url(s, file, digest, digest_type)) {
  -					found = 1;
  -				}
  -				xfree(s);
  -				if (found)
  +			for (i = 0; i < data->reference_count; i++) {
  +				if (!get_url(data->references[i],
  +					     data->file,
  +					     digest,
  +					     digest_type))
   					break;
   			}
   
  -			if (!found) {
  -				Nprint_h_err("Unable to download file %s.",
  -					     file);
  -				goto free_all_and_return;
  +			if (i < data->reference_count) {
  +				status = 0;
  +			} else {
  +				Nprint_h_err("Unable to download file %s.", data->file);
   			}
   		} else {
  -			Nprint_h_err("Unable to download file %s.", file);
  -			goto free_all_and_return;
  +			Nprint_h_err("Checking for %s failed:", data->file);
  +			Nprint_h_err("    %s", strerror(errno));
   		}
  -	} else if (digest && verify_digest(digest_type, digest, file)) {
  -		Nprint_h_err("Wrong %s digest of file: %s", digest_type,
  -			     file);
  -		goto free_all_and_return;
  +	} else if (digest && verify_digest(digest_type, digest, data->file)) {
  +		Nprint_h_err("Wrong %s digest of file: %s", digest_type, data->file);
  +	} else {
  +		status = 0;
   	}
   
  -	decompressor = alloc_decompress_command(get_compression_type(file));
  -
  -	if (decompressor == NULL)
  -		decompressor = xstrdup("cat %s");
  -	append_str(&decompressor, " | %s");
  +	if (status == 0) {
  +		decompressor = alloc_decompress_command(get_compression_type(data->file));
  +		if (decompressor == NULL)
  +			decompressor = xstrdup("cat %s");
  +		append_str(&decompressor, " | %spatch -p%d %s%s");
  +
  +		Nprint_h("    %spatch -p%d %s%s", data->prefix, data->path_strip,
  +			 data->forward_mode ? "-N" : "-R", data->param);
  +
  +		if ((status = execute_command(element, decompressor, data->file,
  +					      data->prefix, data->path_strip,
  +					      data->forward_mode ? "-N" : "-R", data->param)))
  +			Nprint_h_err("Patching failed.");
   
  -	command = xstrdup("");
  -
  -	append_prefix_elements(&command, el);
  -
  -	append_str(&command, "patch -p");
  -
  -	path_strip = attr_value("path_strip", el);
  -	if (path_strip)
  -		append_str(&command, path_strip);
  -	else
  -		append_str(&command, "1");
  -	
  -	mode = attr_value("mode", el);
  -	if (mode) {
  -		if (strcmp(mode, "forward") == 0)
  -			append_str(&command, " -N");
  -		else if (strcmp(mode, "reverse") == 0)
  -			append_str(&command, " -R");
  -		else {
  -			Nprint_h_err("Unknown mode specified: %s", mode);
  -			goto free_all_and_return;
  -		}
  -	} else
  -		append_str(&command, " -N");
  -
  -	append_param_elements(&command, el);
  -
  -	Nprint_h("    %s", command);
  -
  -	if ((status = execute_command(el, decompressor, file,
  -				      command))) {
  -		Nprint_h_err("Patching failed.");
  +		xfree(decompressor);
   	}
   	
  -free_all_and_return:
  -	xfree(decompressor);
   	xfree(digest_type);
   	xfree(digest);
  -	xfree(file);
  -	xfree(command);
   
   	return status;
   }
  @@ -292,6 +395,10 @@
   		.main = patch_main_ver2,
   		.type = HTYPE_NORMAL,
   		.is_action = 1,
  +		.setup = patch_setup,
  +		.free = patch_free,
  +		.valid_data = patch_valid_data,
  +		.parameter = patch_parameter,
   	},
   #endif
   #if HANDLER_SYNTAX_3_0
  @@ -304,6 +411,11 @@
   		.main = patch_main_ver3,
   		.type = HTYPE_NORMAL,
   		.is_action = 1,
  +		.setup = patch_setup,
  +		.free = patch_free,
  +		.parameter = patch_parameter,
  +		.valid_data = patch_valid_data,
  +		.attribute = patch_attribute,
   	},
   #endif
   #if HANDLER_SYNTAX_3_1
  @@ -317,6 +429,11 @@
   		.type = HTYPE_NORMAL,
   		.alloc_data = NULL,
   		.is_action = 1,
  +		.setup = patch_setup,
  +		.free = patch_free,
  +		.valid_data = patch_valid_data,
  +		.parameter = patch_parameter,
  +		.attribute = patch_attribute,
   	},
   #endif
   #if HANDLER_SYNTAX_3_2
  @@ -330,6 +447,12 @@
   		.type = HTYPE_NORMAL,
   		.is_action = 1,
   		.alternate_shell = 1,
  +		.setup = patch_setup,
  +		.free = patch_free,
  +		.valid_data = patch_valid_data_v3_2,
  +		.parameter = patch_parameter,
  +		.attribute = patch_attribute,
  +		.valid_child = patch_valid_child_v3_2,
   	},
   #endif
   	{
  
  
  



More information about the alfs-log mailing list