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

kpfleming at linuxfromscratch.org kpfleming at linuxfromscratch.org
Tue Mar 2 20:54:03 PST 2004


kpfleming    04/03/02 21:54:03

  Modified:    nALFS/src/handlers link.c
  Log:
  convert to new parsing system
  
  Revision  Changes    Path
  1.20      +221 -157  ALFS/nALFS/src/handlers/link.c
  
  Index: link.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/link.c,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- link.c	29 Feb 2004 04:24:59 -0000	1.19
  +++ link.c	3 Mar 2004 04:54:03 -0000	1.20
  @@ -40,103 +40,199 @@
   #include "backend.h"
   
   
  -#define El_link_target(el) alloc_trimmed_param_value("target", el)
  -#define El_link_name(el) alloc_trimmed_param_value("name", el)
  -
  -
  -#if HANDLER_SYNTAX_2_0
  -
  -static const struct handler_parameter link_parameters_v2[] = {
  -	{ .name = "base" },
  -	{ .name = "options" },
  -	{ .name = "target" },
  -	{ .name = "name" },
  -	{ .name = NULL }
  +enum {
  +	LINK_BASE,
  +	LINK_OPTIONS,
  +	LINK_OPTION,
  +	LINK_TARGET,
  +	LINK_NAME,
  +	LINK_TYPE,
   };
   
  -static const struct handler_attribute link_attributes_v2[] = {
  -	{ .name = "type" },
  -	{ .name = NULL }
  +struct link_data {
  +	char *base;
  +	char *name;
  +	int target_count;
  +	char **targets;
  +	int force;
  +	int no_dereference;
  +	int hard_link;
   };
   
  -static int link_main_ver2(const element_s * const el)
  +static int link_setup(element_s * const element)
   {
  -	int status;
  -	int force = option_exists("force", el);
  -	char *type = attr_value("type", el);
  -	char *base;
  -	char *target;
  -	char *link_name;
  -	char *command = NULL;
  -	char *message = NULL;
  +	struct link_data *data;
   
  +	if ((data = xmalloc(sizeof(struct link_data))) == NULL)
  +		return 1;
   
  -	if ((target = El_link_target(el)) == NULL) {
  -		Nprint_h_err("No source files specified.");
  -		return -1;
  -	}
  +	data->hard_link = 0;
  +	data->force = 0;
  +	data->no_dereference = 0;
  +	data->base = NULL;
  +	data->name = NULL;
  +	data->target_count = 0;
  +	data->targets = NULL;
  +	element->handler_data = data;
   
  -	link_name = El_link_name(el);
  +	return 0;
  +};
   
  -	base = alloc_base_dir(el);
  +static void link_free(const element_s * const element)
  +{
  +	struct link_data *data = (struct link_data *) element->handler_data;
  +	int i;
   
  -	if (change_current_dir(base)) {
  -		xfree(base);
  -		xfree(target);
  -		xfree(link_name);
  -		return -1;
  +	xfree(data->base);
  +	xfree(data->name);
  +	if (data->target_count > 0) {
  +		for (i = 0; i < data->target_count; i++)
  +			xfree(data->targets[i]);
  +		xfree(data->targets);
   	}
  +	xfree(data);
  +}
   
  -	if (type == NULL || strcmp(type, "symbolic") == 0) {
  -		append_str(&command, "ln -s");
  -		append_str(&message, "Creating symbolic link in ");
  -
  -	} else if (strcmp(type, "hard") == 0) {
  -		append_str(&command, "ln");
  -		append_str(&message, "Creating hard link in ");
  +static int link_attribute(const element_s * const element,
  +			  const struct handler_attribute * const attr,
  +			  const char * const value)
  +{
  +	struct link_data *data = (struct link_data *) element->handler_data;
   
  -	} else {
  -		Nprint_h_warn("Unknown link type (%s), using symbolic.", type);
  -		append_str(&command, "ln -s");
  -		append_str(&message, "Creating symbolic link in ");
  +	switch (attr->private) {
  +	case LINK_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 LINK_TYPE:
  +		if (!strcmp("symbolic", value)) {
  +			data->hard_link = 0;
  +		} else if (!strcmp("hard", value)) {
  +			data->hard_link = 1;
  +		} else {
  +			Nprint_err("<%s>: \"type\" must be \"symbolic\" or \"hard\".", element->handler->name);
  +			return 0;
  +		}
  +	default:
  +		return 1;
   	}
  +}
   
  -	append_str(&message, base);
  -
  -	if (force) {
  -		append_str(&command, " -f");
  -		append_str(&message, " (force):");
  +static int link_parameter(const element_s * const element,
  +			  const struct handler_parameter * const param,
  +			  const char * const value)
  +{
  +	struct link_data *data = (struct link_data *) element->handler_data;
   
  -	} else {
  -		append_str(&message, ":");
  +	switch (param->private) {
  +	case LINK_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 LINK_OPTIONS:
  +		if (option_in_string("force", value)) {
  +			data->force = 1;
  +			return 0;
  +		} else {
  +			Nprint_err("<%s>: invalid options in (%s) ignored", element->handler->name, value);
  +			return 1;
  +		}
  +	case LINK_OPTION:
  +		if (!strcmp("force", value)) {
  +			data->force = 1;
  +			return 0;
  +		}
  +		if (!strcmp("no-dereference", value)) {
  +			data->no_dereference = 1;
  +			return 0;
  +		}
  +		Nprint_err("<%s>: invalid option (%s) ignored", element->handler->name, value);
  +		return 1;
  +	case LINK_TARGET:
  +		data->target_count++;
  +		if ((data->targets = xrealloc(data->targets,
  +					      sizeof(data->targets[0]) * (data->target_count))) == NULL) {
  +			Nprint_err("xrealloc() failed: %s", strerror(errno));
  +			return -1;
  +		}
  +		data->targets[(data->target_count - 1)] = xstrdup(value);
  +		return 0;
  +	case LINK_NAME:
  +		if (data->name) {
  +			Nprint_err("<%s>: cannot specify <name> more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->name = xstrdup(value);
  +		return 0;
  +	default:
  +		return 1;
   	}
  +}
   
  -	Nprint_h("%s", message);
  +static int link_valid_data(const element_s * const element)
  +{
  +	struct link_data *data = (struct link_data *) element->handler_data;
   
  -	if (link_name) {
  -		Nprint_h("    %s -> %s", link_name, target);
  -	} else {
  -		Nprint_h("    %s", target);
  +	if (data->target_count == 0) {
  +		Nprint_err("<%s>: <target> must be specified.", element->handler->name);
  +		return 0;
   	}
   
  -	append_str(&command, " ");
  -	append_str(&command, target);
  +	return 1;
  +}
   
  -	if (link_name) {
  -		append_str(&command, " ");
  -		append_str(&command, link_name);
  -	}
  +#if HANDLER_SYNTAX_2_0
   
  -	if ((status = execute_command(el, "%s", command))) {
  -		Nprint_h_err("Executing \"%s\" in \"%s\" failed.",
  -			command, base);
  +static const struct handler_parameter link_parameters_v2[] = {
  +	{ .name = "base", .private = LINK_BASE },
  +	{ .name = "options", .private = LINK_OPTIONS },
  +	{ .name = "target", .private = LINK_TARGET },
  +	{ .name = "name", .private = LINK_NAME },
  +	{ .name = NULL }
  +};
  +
  +static const struct handler_attribute link_attributes_v2[] = {
  +	{ .name = "type", .private = LINK_TYPE },
  +	{ .name = NULL }
  +};
  +
  +static int link_main_ver2(const element_s * const element)
  +{
  +	struct link_data *data = (struct link_data *) element->handler_data;
  +	int status;
  +
  +	if (change_to_base_dir(element, data->base, 1))
  +		return -1;
  +
  +	if (data->name) {
  +		Nprint_h("Creating %s link %s%s -> %s",
  +			 data->hard_link ? "hard" : "symbolic",
  +			 data->force ? "(force) " : "",
  +			 data->name, data->targets[0]);
  +		status = execute_command(element, "ln %s %s %s %s",
  +					 data->hard_link ? "" : "-s",
  +					 data->force ? "-f" : "",
  +					 data->targets[0],
  +					 data->name);
  +	} else {
  +		Nprint_h("Creating %s link %s%s",
  +			 data->hard_link ? "hard" : "symbolic",
  +			 data->force ? "(force) " : "",
  +			 data->targets[0]);
  +		status = execute_command(element, "ln %s %s %s",
  +					 data->hard_link ? "" : "-s",
  +					 data->force ? "-f" : "",
  +					 data->targets[0]);
   	}
   
  -	xfree(base);
  -	xfree(target);
  -	xfree(link_name);
  -	xfree(command);
  -	xfree(message);
  +	if (status)
  +		Nprint_h_err("Creating link failed.");
   
   	return status;
   }
  @@ -159,103 +255,51 @@
   	{ .name = NULL }
   };
   
  -static int link_main_ver3(const element_s * const el)
  +static int link_main_ver3(const element_s * const element)
   {
  -	int options[2], force, no_dereference;
  +	struct link_data *data = (struct link_data *) element->handler_data;
   	int status;
  -	char *type;
   	char *targets = NULL;
  -	char *link_name;
  -	char *command = NULL;
  -	char *message = NULL;
  -	element_s *p;
  -
  +	int i;
   
  -	if (change_to_base_dir(el, attr_value("base", el), 1))
  +	if (change_to_base_dir(element, data->base, 1))
   		return -1;
   
  -	/* Read all <option>s. */
  -	check_options(2, options, "force no-dereference", el);
  -	force = options[0];
  -	no_dereference = options[1];
  -
  -	link_name = alloc_trimmed_param_value("name", el);
  -
  -	type = attr_value("type", el);
  -
  -	if (type == NULL || strcmp(type, "symbolic") == 0) {
  -		append_str(&message, "Creating symbolic link in ");
  -		append_str(&command, "ln -s");
  -
  -	} else if (strcmp(type, "hard") == 0) {
  -		append_str(&message, "Creating hard link in ");
  -		append_str(&command, "ln");
  -
  -	} else {
  -		Nprint_h_warn("Unknown link type (%s), using symbolic.", type);
  -		append_str(&message, "Creating symbolic link");
  -		append_str(&command, "ln -s");
  -	}
  -
  -	if (force) {
  -		append_str(&message, " (force)");
  -		append_str(&command, " -f");
  -	}
  -
  -	if (no_dereference) {
  -		append_str(&message, " (no_dereference)");
  -		append_str(&command, " -n");
  -	}
  -
  -	append_str(&message, ": ");
  -
  -	if (link_name) {
  -		append_str(&message, link_name);
  -	}
  -
  -	/* Concatenate all targets in "targets". */
  -	for (p = first_param("target", el); p; p = next_param(p)) {
  -		char *target;
  -
  -
  -		if ((target = alloc_trimmed_str(p->content)) == NULL) {
  -			Nprint_h_warn("No target specified.");
  -			continue;
  -		}
  -
  -		if (targets != NULL) {
  -			append_str(&targets, " ");
  -		}
  -
  -		append_str(&targets, target);
  -
  -		xfree(target);
  +	for (i = 0; i < data->target_count; i++) {
  +		append_str(&targets, data->targets[i]);
  +		append_str(&targets, " ");
   	}
   
  -	if (targets) {
  -		append_str(&command, " ");
  -		append_str(&command, targets);
  -
  -		if (link_name) {
  -			append_str(&command, " ");
  -			append_str(&command, link_name);
  -		}
  -
  -		Nprint_h("%s", message);
  +	if (data->name) {
  +		Nprint_h("Creating %s link %s%s%s",
  +			 data->hard_link ? "hard" : "symbolic",
  +			 data->force ? "(force) " : "",
  +			 data->no_dereference ? "(no-dereference) " : "",
  +			 data->name);
   		Nprint_h("    %s", targets);
  -
  -		if ((status = execute_command(el, "%s", command))) {
  -			Nprint_h_err("Executing \"%s\" failed.", command);
  -		}
  -
  +		status = execute_command(element, "ln %s %s %s %s %s",
  +					 data->hard_link ? "" : "-s",
  +					 data->force ? "-f" : "",
  +					 data->no_dereference ? "-n" : "",
  +					 targets,
  +					 data->name);
   	} else {
  -		Nprint_h_err("No target for link specified.");
  -		status = -1;
  +		Nprint_h("Creating %s link %s%s",
  +			 data->hard_link ? "hard" : "symbolic",
  +			 data->force ? "(force) " : "",
  +			 data->no_dereference ? "(no-dereference) " : "");
  +		Nprint_h("    %s", targets);
  +		status = execute_command(element, "ln %s %s %s %s",
  +					 data->hard_link ? "" : "-s",
  +					 data->force ? "-f" : "",
  +					 data->no_dereference ? "-n" : "",
  +					 targets);
   	}
   
  -	xfree(link_name);
  -	xfree(command);
  -	xfree(message);
  +	xfree(targets);
  +
  +	if (status)
  +		Nprint_h_err("Creating link failed.");
   
   	return status;
   }
  @@ -278,6 +322,11 @@
   		.main = link_main_ver2,
   		.type = HTYPE_NORMAL,
   		.is_action = 1,
  +		.setup = link_setup,
  +		.free = link_free,
  +		.attribute = link_attribute,
  +		.parameter = link_parameter,
  +		.valid_data = link_valid_data,
   	},
   #endif
   #if HANDLER_SYNTAX_3_0
  @@ -290,6 +339,11 @@
   		.main = link_main_ver3,
   		.type = HTYPE_NORMAL,
   		.is_action = 1,
  +		.setup = link_setup,
  +		.free = link_free,
  +		.attribute = link_attribute,
  +		.parameter = link_parameter,
  +		.valid_data = link_valid_data,
   	},
   #endif
   #if HANDLER_SYNTAX_3_1
  @@ -302,6 +356,11 @@
   		.main = link_main_ver3,
   		.type = HTYPE_NORMAL,
   		.is_action = 1,
  +		.setup = link_setup,
  +		.free = link_free,
  +		.attribute = link_attribute,
  +		.parameter = link_parameter,
  +		.valid_data = link_valid_data,
   	},
   #endif
   #if HANDLER_SYNTAX_3_2
  @@ -315,6 +374,11 @@
   		.type = HTYPE_NORMAL,
   		.is_action = 1,
   		.alternate_shell = 1,
  +		.setup = link_setup,
  +		.free = link_free,
  +		.attribute = link_attribute,
  +		.parameter = link_parameter,
  +		.valid_data = link_valid_data,
   	},
   #endif
   	{
  
  
  



More information about the alfs-log mailing list