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

kpfleming at linuxfromscratch.org kpfleming at linuxfromscratch.org
Sat Feb 7 18:21:36 PST 2004


kpfleming    04/02/07 19:21:36

  Modified:    nALFS/src/handlers execute.c
  Log:
  add syntax version 3.2 for <execute>, supporting <content> element to specify shell script to be executed (mutually exclusive of "command" attribute)
  remove extra parameters from execute syntax versions 3.0/3.1 (base & command)
  
  Revision  Changes    Path
  1.17      +117 -6    ALFS/nALFS/src/handlers/execute.c
  
  Index: execute.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/execute.c,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- execute.c	6 Feb 2004 05:54:36 -0000	1.16
  +++ execute.c	8 Feb 2004 02:21:36 -0000	1.17
  @@ -21,9 +21,13 @@
    */
   
   
  +#include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <errno.h>
  +#include <unistd.h>
  +#include <sys/types.h>
  +#include <sys/stat.h>
   
   #ifdef HAVE_CONFIG_H
   #include <config.h>
  @@ -37,6 +41,7 @@
   #include "nprint.h"
   #include "parser.h"
   #include "backend.h"
  +#include "options.h"
   
   
   #if HANDLER_SYNTAX_2_0
  @@ -88,10 +93,10 @@
   #endif /* HANDLER_SYNTAX_2_0 */
   
   
  -#if HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 || HANDLER_SYNTAX_3_2
  +#if HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1
   
   static const char *execute_parameters_ver3[] =
  -{ "base", "command", "param", "prefix", NULL };
  +{ "param", "prefix", NULL };
   // char *HANDLER_SYMBOL(attributes)[] = { "base", "command", NULL };
   
   static int execute_main_ver3(element_s *el)
  @@ -145,7 +150,113 @@
   	return NULL;
   }
   
  -#endif /* HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 || HANDLER_SYNTAX_3_2 */
  +#endif /* HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 */
  +
  +
  +#ifdef HANDLER_SYNTAX_3_2
  +
  +static const char *execute_parameters_ver3_2[] =
  +{ "param", "prefix", "content", NULL };
  +// char *HANDLER_SYMBOL(attributes)[] = { "base", "command", NULL };
  +
  +static int execute_main_ver3_2(element_s *el)
  +{
  +	int status = -1;
  +	char *base;
  +	char *c;
  +	char *content = NULL;
  +
  +	c = attr_value("command", el);
  +	content = alloc_trimmed_param_value("content", el);
  +
  +	if (!(c || content)) {
  +		Nprint_h_err("Either command or <content> must be specified.");
  +		return -1;
  +	}
  +	
  +	if (c && content) {
  +		Nprint_h_err("Cannot specify both command and <content>.");
  +		return -1;
  +	}
  +
  +	if (content &&
  +	    (first_param("prefix", el) || first_param("param", el))) {
  +		Nprint_h_err("Cannot specify both <content> and <param>/<prefix>.");
  +		return -1;
  +	}
  +
  +	base = alloc_base_dir_new(el);
  +
  +	if (change_current_dir(base)) {
  +		xfree(base);
  +		return -1;
  +	}
  +
  +	if (c) {
  +		char *command;
  +
  +		command = xstrdup("");
  +		append_prefix_elements(&command, el);
  +		append_str(&command, c);
  +		append_param_elements(&command, el);
  +		Nprint_h("Executing system command in %s:", base);
  +		Nprint_h("    %s", command);
  +		status = execute_command("%s", command);
  +		xfree(command);
  +	} else {
  +		FILE *temp_script;
  +		char *tok;
  +		char *temp_file_name;
  +
  +		temp_file_name = xstrdup(*opt_alfs_directory);
  +		append_str(&temp_file_name, "/");
  +		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")) {
  +					fprintf(temp_script, "%s\n", ++tok);
  +				}
  +				fclose(temp_script);
  +				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));
  +				} else {
  +					Nprint_h("Executing script in %s:", base);
  +					status = execute_command(temp_file_name);
  +				}
  +				unlink(temp_file_name);
  +			} else {
  +				Nprint_h_err("Cannot open %s for writing.", temp_file_name);
  +			}
  +		}
  +		xfree(temp_file_name);
  +	}
  +
  +	xfree(base);
  +	xfree(content);
  +
  +	return status;
  +}
  +
  +static char *execute_data_ver3_2(element_s *el, handler_data_e data)
  +{
  +	char *command;
  +	element_s *content_param;
  +
  +	(void) data;
  +
  +	if ((command = attr_value("command", el))) {
  +		return xstrdup(command);
  +	} else if ((content_param = first_param("content", el))) {
  +		return alloc_trimmed_str(content_param->content);
  +	}
  +
  +	return NULL;
  +}
  +
  +#endif
   
   
   /*
  @@ -197,10 +308,10 @@
   		.name = "execute",
   		.description = "Execute",
   		.syntax_version = "3.2",
  -		.parameters = execute_parameters_ver3,
  -		.main = execute_main_ver3,
  +		.parameters = execute_parameters_ver3_2,
  +		.main = execute_main_ver3_2,
   		.type = HTYPE_EXECUTE,
  -		.alloc_data = execute_data_ver3,
  +		.alloc_data = execute_data_ver3_2,
   		.is_action = 1,
   		.priority = 0
   	},
  
  
  



More information about the alfs-log mailing list