cvs commit: ALFS/nALFS/src/handlers su.c textdump.c

kpfleming at linuxfromscratch.org kpfleming at linuxfromscratch.org
Mon Mar 1 21:18:06 PST 2004


kpfleming    04/03/01 22:18:06

  Modified:    nALFS/src/handlers su.c textdump.c
  Log:
  convert to new parsing system
  
  Revision  Changes    Path
  1.21      +90 -26    ALFS/nALFS/src/handlers/su.c
  
  Index: su.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/su.c,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- su.c	2 Mar 2004 04:05:53 -0000	1.20
  +++ su.c	2 Mar 2004 05:18:06 -0000	1.21
  @@ -49,7 +49,81 @@
   #include "utility.h"
   
   
  -#if HANDLER_SYNTAX_2_0
  +enum {
  +	SU_USER,
  +};
  +
  +static const struct handler_attribute su_attributes[] = {
  +	{ .name = "user", .private = SU_USER },
  +	{ .name = NULL }
  +};
  +
  +struct su_data {
  +	char *user;
  +};
  +
  +static int su_setup(element_s * const element)
  +{
  +	struct su_data *data;
  +
  +	if ((data = xmalloc(sizeof(struct su_data))) == NULL)
  +		return -1;
  +
  +	data->user = NULL;
  +	element->handler_data = data;
  +
  +	return 0;
  +}
  +
  +static void su_free(const element_s * const element)
  +{
  +	struct su_data *data = (struct su_data *) element->handler_data;
  +
  +	xfree(data->user);
  +	xfree(data);
  +}
  +
  +static int su_attribute(const element_s * const element,
  +			const struct handler_attribute * const attr,
  +			const char * const value)
  +{
  +	struct su_data *data = (struct su_data *) element->handler_data;
  +
  +	switch (attr->private) {
  +	case SU_USER:
  +		if (data->user) {
  +			Nprint_err("<%s>: cannot specify \"user\" more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->user = xstrdup(value);
  +		return 0;
  +	default:
  +		return 1;
  +	}
  +}
  +
  +static int su_valid_data(const element_s * const element)
  +{
  +	struct su_data *data = (struct su_data *) element->handler_data;
  +
  +	if (!data->user) {
  +		Nprint_err("<%s>: \"user\" must be specified.", element->handler->name);
  +		return 0;
  +	}
  +
  +	return 1;
  +}
  +
  +static int su_valid_child(const element_s * const element, const element_s * const child)
  +{
  +	(void) element;
  +
  +	return child->handler->type & (HTYPE_NORMAL |
  +				       HTYPE_COMMENT |
  +				       HTYPE_TEXTDUMP |
  +				       HTYPE_PACKAGE |
  +				       HTYPE_EXECUTE);
  +}
   
   static INLINE int set_supplementary_groups(const char *user)
   {
  @@ -138,32 +212,20 @@
   	return 0;
   }
   
  -
  -static const struct handler_attribute su_attributes[] = {
  -	{ .name = "user" },
  -	{ .name = NULL }
  -};
  -
  -static int su_main(const element_s * const el)
  +static int su_main(const element_s * const element)
   {
  +	struct su_data *data = (struct su_data *) element->handler_data;
   	int status;
   	pid_t su_pid, got_pid;
  -        char *user = attr_value("user", el);
  -
   	
  -        if (user == NULL) {
  -                Nprint_h_err("User not specified for su.");
  -                return -1;
  -	}
  -
   	su_pid = fork();
   
   	if (su_pid == 0) { /* Child. */
  -		if (change_to_user(user) == -1) {
  +		if (change_to_user(data->user) == -1) {
   			exit(EXIT_FAILURE);
   		}
   
  -		status = execute_children(el);
  +		status = execute_children(element);
   
   		/* Nprint_h("Exited from su (%s).", pw->pw_name); */
   		
  @@ -175,7 +237,7 @@
   
   	if ((got_pid = waitpid(su_pid, &status, WUNTRACED)) == -1) {
   		fatal_backend_error("waitpid() for %ld in su failed.",
  -			(long)su_pid);
  +				    (long)su_pid);
   	}
   
   	if (WIFEXITED(status)) { /* Child exited normally. */
  @@ -183,27 +245,24 @@
   
   	} else if (WIFSIGNALED(status)) {
   		Nprint_h_err("Su child (%ld) killed by signal %d%s.",
  -			(long)got_pid, WTERMSIG(status),
  -			WCOREDUMP(status) ? " (core dumped)" : "");
  +			     (long)got_pid, WTERMSIG(status),
  +			     WCOREDUMP(status) ? " (core dumped)" : "");
   		status = -1;
   
   	} else if (WIFSTOPPED(status)) {
   		Nprint_h_err("Su child (%ld) stopped by signal %d.",
  -			(long)got_pid, WSTOPSIG(status));
  +			     (long)got_pid, WSTOPSIG(status));
   		status = -1;
   
   	} else {
   		Nprint_h_err("Su child (%ld) exited abnormaly.",
  -			(long)got_pid);
  +			     (long)got_pid);
   		status = -1;
   	}
   	
   	return status;
   }
   
  -#endif /* HANDLER_SYNTAX_2_0 */
  -
  -
   /*
    * Handlers' information.
    */
  @@ -214,9 +273,14 @@
   		.name = "su",
   		.description = "Change user ID",
   		.syntax_version = "2.0",
  -		.main = su_main,
   		.type = HTYPE_NORMAL,
  +		.main = su_main,
   		.attributes = su_attributes,
  +		.setup = su_setup,
  +		.free = su_free,
  +		.attribute = su_attribute,
  +		.valid_data = su_valid_data,
  +		.valid_child = su_valid_child,
   	},
   #endif
   	{
  
  
  
  1.22      +182 -104  ALFS/nALFS/src/handlers/textdump.c
  
  Index: textdump.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/textdump.c,v
  retrieving revision 1.21
  retrieving revision 1.22
  diff -u -r1.21 -r1.22
  --- textdump.c	29 Feb 2004 04:24:59 -0000	1.21
  +++ textdump.c	2 Mar 2004 05:18:06 -0000	1.22
  @@ -40,137 +40,195 @@
   #include "nprint.h"
   #include "parser.h"
   
  +enum {
  +	TEXTDUMP_BASE,
  +	TEXTDUMP_FILE,
  +	TEXTDUMP_CONTENT,
  +	TEXTDUMP_MODE,
  +};
   
  -#define El_textdump_content(el) alloc_trimmed_param_value("content", el)
  -
  -
  -static INLINE FILE *open_for_append(const char *file, const char *base)
  -{
  -	Nprint_h("Appending text to \"%s\" (%s).", file, base);
  -	return fopen(file, "a");
  -}
  -
  -static INLINE FILE *open_for_overwrite(const char *file, const char *base)
  -{
  -	Nprint_h("Creating new file in %s: %s", base, file);
  -	return fopen(file, "w");
  -}
  -
  -static int textdump_main(element_s *el, const char *base_dir)
  -{
  -	char *tok;
  +struct textdump_data {
  +	char *base;
   	char *file;
   	char *content;
  -	char *mode = attr_value("mode", el);
  -	FILE *fp;
  -
  -
  -	if ((file = alloc_textdump_file(el)) == NULL) {
  -		Nprint_h_err("No file for textdump specified.");
  -		return -1;
  -	}
  -
  -	if ((content = El_textdump_content(el)) == NULL) {
  -		Nprint_h_err("No content for textdump specified.");
  -		xfree(file);
  -		return -1;
  -	}
  -
  -	if (change_current_dir(base_dir)) {
  -		xfree(file);
  -		xfree(content);
  -		return -1;
  -	}
  -
  -	if (mode && strcmp(mode, "append") == 0) {
  -		fp = open_for_append(file, base_dir);
  -	} else {
  -		fp = open_for_overwrite(file, base_dir);
  -	}
  -
  -	if (fp == NULL) {
  -		Nprint_h_err("Unable to open \"%s\":", file);
  -		Nprint_h_err("    %s", strerror(errno));
  -		xfree(file);
  -		xfree(content);
  -		return -1;
  -	}
  -
  -	for (tok = strtok(content, "\n"); tok; tok = strtok(NULL, "\n")) {
  -		fprintf(fp, "%s\n", ++tok);
  -	}
  -
  -	fclose(fp);
  -
  -	xfree(file);
  -	xfree(content);
  -	
  -	return 0;
  -}
  -
  +	int append_mode;
  +};
   
   #if HANDLER_SYNTAX_2_0
   
   static const struct handler_parameter textdump_parameters_v2[] = {
  -	{ .name = "base" },
  -	{ .name = "file" },
  -	{ .name = "content" },
  +	{ .name = "base", .private = TEXTDUMP_BASE },
  +	{ .name = "file", .private = TEXTDUMP_FILE },
  +	{ .name = "content", .private = TEXTDUMP_CONTENT },
   	{ .name = NULL }
   };
   
   static const struct handler_attribute textdump_attributes_v2[] = {
  -	{ .name = "mode" },
  +	{ .name = "mode", .private = TEXTDUMP_MODE },
   	{ .name = NULL }
   };
   
  -static int textdump_main_ver2(const element_s * const el)
  -{
  -	char *base = alloc_base_dir(el);
  -	int result;
  -
  -	result = textdump_main(el, base);
  -	xfree(base);
  -	return result;
  -}
  -
   #endif /* HANDLER_SYNTAX_2_0 */
   
  -
   #if HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 || HANDLER_SYNTAX_3_2
   
   static const struct handler_parameter textdump_parameters_v3[] = {
  -	{ .name = "file" },
  -	{ .name = "content" },
  +	{ .name = "file", .private = TEXTDUMP_FILE },
  +	{ .name = "content", .private = TEXTDUMP_CONTENT },
   	{ .name = NULL }
   };
   
   static const struct handler_attribute textdump_attributes_v3[] = {
  -	{ .name = "base" },
  -	{ .name = "mode" },
  +	{ .name = "base", .private = TEXTDUMP_BASE },
  +	{ .name = "mode", .private = TEXTDUMP_MODE },
   	{ .name = NULL }
   };
   
  -static int textdump_main_ver3(const element_s * const el)
  +#endif /* HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 || HANDLER_SYNTAX_3_2 */
  +
  +static int textdump_setup(element_s * const element)
   {
  -	if (change_to_base_dir(el, attr_value("base", el), 1))
  +	struct textdump_data *data;
  +
  +	if ((data = xmalloc(sizeof(struct textdump_data))) == NULL)
   		return -1;
   
  -	return textdump_main(el, ".");
  +	data->base = NULL;
  +	data->file = NULL;
  +	data->content = NULL;
  +	data->append_mode = 0;
  +	element->handler_data = data;
  +
  +	return 0;
   }
   
  +static void textdump_free(const element_s * const element)
  +{
  +	struct textdump_data *data = (struct textdump_data *) element->handler_data;
   
  -#endif /* HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 || HANDLER_SYNTAX_3_2 */
  +	xfree(data->base);
  +	xfree(data->file);
  +	xfree(data->content);
  +	xfree(data);
  +}
   
  +static int textdump_attribute(const element_s * const element,
  +			      const struct handler_attribute * const attr,
  +			      const char * const value)
  +{
  +	struct textdump_data *data = (struct textdump_data *) element->handler_data;
  +
  +	switch (attr->private) {
  +	case TEXTDUMP_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 TEXTDUMP_MODE:
  +		if (!strcmp(value, "append")) {
  +			Nprint_err("<%s>: the only \"mode\" allowed is \"append\".", element->handler->name);
  +			return 1;
  +		}
  +		data->append_mode = 1;
  +		return 0;
  +	default:
  +		return 1;
  +	}
  +}
  +
  +static int textdump_parameter(const element_s * const element,
  +			    const struct handler_parameter * const param,
  +			    const char * const value)
  +{
  +	struct textdump_data *data = (struct textdump_data *) element->handler_data;
  +
  +	switch (param->private) {
  +	case TEXTDUMP_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 TEXTDUMP_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 TEXTDUMP_CONTENT:
  +		if (data->content) {
  +			Nprint_err("<%s>: cannot specify <content> more than once.", element->handler->name);
  +			return 1;
  +		}
  +		data->content = xstrdup(value);
  +		return 0;
  +	default:
  +		return 1;
  +	}
  +}
   
  -static char *textdump_data(const element_s * const el,
  -			   const handler_data_e data)
  +static int textdump_valid_data(const element_s * const element)
   {
  -	(void) data;
  +	struct textdump_data *data = (struct textdump_data *) element->handler_data;
  +
  +	if (!data->file) {
  +		Nprint_err("<%s>: <file> must be specified.", element->handler->name);
  +		return 0;
  +	}
  +
  +	if (!data->content) {
  +		Nprint_err("<%s>: <content> must be specified.", element->handler->name);
  +		return 0;
  +	}
  +
  +	return 1;
  +}
  +
  +static int textdump_main(const element_s * const element)
  +{
  +	struct textdump_data *data = (struct textdump_data *) element->handler_data;
  +	char *tok;
  +	FILE *fp;
  +
  +	if (change_to_base_dir(element, data->base, 1))
  +		return -1;
  +
  +	if (data->append_mode) {
  +		Nprint_h("Appending text to \"%s\".", data->file);
  +		fp = fopen(data->file, "a");
  +	} else {
  +		Nprint_h("Creating new file: %s", data->file);
  +		fp = fopen(data->file, "w");
  +	}
  +
  +	if (fp == NULL) {
  +		Nprint_h_err("Unable to open \"%s\":", data->file);
  +		Nprint_h_err("    %s", strerror(errno));
  +		return -1;
  +	}
   
  -	return alloc_trimmed_param_value("file", el);
  +	for (tok = strtok(data->content, "\n"); tok; tok = strtok(NULL, "\n")) {
  +		fprintf(fp, "%s\n", ++tok);
  +	}
  +
  +	fclose(fp);
  +	
  +	return 0;
   }
   
  +static char *textdump_data(const element_s * const element,
  +			   const handler_data_e data_requested)
  +{
  +	struct textdump_data *data = (struct textdump_data *) element->handler_data;
  +
  +	(void) data_requested;
   
  +	return xstrdup(data->file);
  +}
   
   /*
    * Handlers' information.
  @@ -182,12 +240,17 @@
   		.name = "textdump",
   		.description = "Dump text",
   		.syntax_version = "2.0",
  -		.parameters = textdump_parameters_v2,
  -		.attributes = textdump_attributes_v2,
  -		.main = textdump_main_ver2,
   		.type = HTYPE_TEXTDUMP,
   		.alloc_data = textdump_data,
   		.is_action = 1,
  +		.main = textdump_main,
  +		.parameters = textdump_parameters_v2,
  +		.attributes = textdump_attributes_v2,
  +		.setup = textdump_setup,
  +		.free = textdump_free,
  +		.attribute = textdump_attribute,
  +		.parameter = textdump_parameter,
  +		.valid_data = textdump_valid_data,
   	},
   #endif
   #if HANDLER_SYNTAX_3_0
  @@ -195,12 +258,17 @@
   		.name = "textdump",
   		.description = "Dump text",
   		.syntax_version = "3.0",
  -		.parameters = textdump_parameters_v3,
  -		.attributes = textdump_attributes_v3,
  -		.main = textdump_main_ver3,
   		.type = HTYPE_TEXTDUMP,
   		.alloc_data = textdump_data,
   		.is_action = 1,
  +		.main = textdump_main,
  +		.parameters = textdump_parameters_v3,
  +		.attributes = textdump_attributes_v3,
  +		.setup = textdump_setup,
  +		.free = textdump_free,
  +		.attribute = textdump_attribute,
  +		.parameter = textdump_parameter,
  +		.valid_data = textdump_valid_data,
   	},
   #endif
   #if HANDLER_SYNTAX_3_1
  @@ -208,12 +276,17 @@
   		.name = "textdump",
   		.description = "Dump text",
   		.syntax_version = "3.1",
  -		.parameters = textdump_parameters_v3,
  -		.attributes = textdump_attributes_v3,
  -		.main = textdump_main_ver3,
   		.type = HTYPE_TEXTDUMP,
   		.alloc_data = textdump_data,
   		.is_action = 1,
  +		.main = textdump_main,
  +		.parameters = textdump_parameters_v3,
  +		.attributes = textdump_attributes_v3,
  +		.setup = textdump_setup,
  +		.free = textdump_free,
  +		.attribute = textdump_attribute,
  +		.parameter = textdump_parameter,
  +		.valid_data = textdump_valid_data,
   	},
   #endif
   #if HANDLER_SYNTAX_3_2
  @@ -221,13 +294,18 @@
   		.name = "textdump",
   		.description = "Dump text",
   		.syntax_version = "3.2",
  -		.parameters = textdump_parameters_v3,
  -		.attributes = textdump_attributes_v3,
  -		.main = textdump_main_ver3,
   		.type = HTYPE_TEXTDUMP,
   		.alloc_data = textdump_data,
   		.is_action = 1,
   		.alternate_shell = 1,
  +		.main = textdump_main,
  +		.parameters = textdump_parameters_v3,
  +		.attributes = textdump_attributes_v3,
  +		.setup = textdump_setup,
  +		.free = textdump_free,
  +		.attribute = textdump_attribute,
  +		.parameter = textdump_parameter,
  +		.valid_data = textdump_valid_data,
   	},
   #endif
   	{
  
  
  



More information about the alfs-log mailing list