[patch] for "psql : Allow processing of multiple -f (file) options "

Started by Vikash3 Salmost 14 years ago2 messages
#1Vikash3 S
vikash3.s@tcs.com
2 attachment(s)

Attachments:

startup.patchapplication/octet-stream; name=startup.patchDownload
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
new file mode 100644
index b5664df..d5793f2
*** a/src/bin/psql/startup.c
--- b/src/bin/psql/startup.c
*************** PsqlSettings pset;

*** 42,48 ****
  #define SYSPSQLRC	"psqlrc"

  #define PSQLRC		"psqlrc.conf"

  #endif

! 

  /*

   * Structures to pass information between the option parsing routine

   * and the main function

--- 42,48 ----
  #define SYSPSQLRC	"psqlrc"

  #define PSQLRC		"psqlrc.conf"

  #endif

! #define NUM_FILES   50

  /*

   * Structures to pass information between the option parsing routine

   * and the main function

*************** struct adhoc_opts

*** 64,70 ****
  	char	   *username;

  	char	   *logfilename;

  	enum _actions action;

! 	char	   *action_string;

  	bool		no_readline;

  	bool		no_psqlrc;

  	bool		single_txn;

--- 64,70 ----
  	char	   *username;

  	char	   *logfilename;

  	enum _actions action;

! 	char	   *action_string[NUM_FILES];

  	bool		no_readline;

  	bool		no_psqlrc;

  	bool		single_txn;

*************** main(int argc, char *argv[])

*** 90,95 ****
--- 90,96 ----
  	char	   *password = NULL;

  	char	   *password_prompt = NULL;

  	bool		new_pass;

+ 	int  i = 0;

  

  	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));

  

*************** main(int argc, char *argv[])

*** 264,270 ****
  		if (!options.no_psqlrc)

  			process_psqlrc(argv[0]);

  

! 		successResult = process_file(options.action_string, options.single_txn, false);

  	}

  

  	/*

--- 265,274 ----
  		if (!options.no_psqlrc)

  			process_psqlrc(argv[0]);

  

! 		for (i=0; i< argc; i++)

! 		{

! 		successResult = process_file(options.action_string[i], options.single_txn,false);

! 		}

  	}

  

  	/*

*************** parse_psql_options(int argc, char *argv[
*** 377,382 ****
--- 381,387 ----
  	extern char *optarg;

  	extern int	optind;

  	int			c;

+ 	static int fileindex = 0;

  

  	memset(options, 0, sizeof *options);

  

*************** parse_psql_options(int argc, char *argv[
*** 392,402 ****
  				pset.popt.topt.format = PRINT_UNALIGNED;

  				break;

  			case 'c':

! 				options->action_string = optarg;

  				if (optarg[0] == '\\')

  				{

  					options->action = ACT_SINGLE_SLASH;

! 					options->action_string++;

  				}

  				else

  					options->action = ACT_SINGLE_QUERY;

--- 397,407 ----
  				pset.popt.topt.format = PRINT_UNALIGNED;

  				break;

  			case 'c':

! 				options->action_string[0] = optarg;

  				if (optarg[0] == '\\')

  				{

  					options->action = ACT_SINGLE_SLASH;

! 					options->action_string[0]++;

  				}

  				else

  					options->action = ACT_SINGLE_QUERY;

*************** parse_psql_options(int argc, char *argv[
*** 412,418 ****
  				break;

  			case 'f':

  				options->action = ACT_FILE;

! 				options->action_string = optarg;

  				break;

  			case 'F':

  				pset.popt.topt.fieldSep.separator = pg_strdup(optarg);

--- 417,423 ----
  				break;

  			case 'f':

  				options->action = ACT_FILE;

! 				options->action_string[fileindex++] = optarg;

  				break;

  			case 'F':

  				pset.popt.topt.fieldSep.separator = pg_strdup(optarg);

getopt_long.patchapplication/octet-stream; name=getopt_long.patchDownload
diff --git a/src/port/getopt_long.c b/src/port/getopt_long.c
new file mode 100644
index d624216..5076bbe
*** a/src/port/getopt_long.c
--- b/src/port/getopt_long.c
*************** getopt_long(int argc, char *const argv[]
*** 60,67 ****
  {

  	static char *place = EMSG;	/* option letter processing */

  	char	   *oli;			/* option letter list index */

! 

! 	if (!*place)

  	{							/* update scanning pointer */

  		if (optind >= argc)

  		{

--- 60,71 ----
  {

  	static char *place = EMSG;	/* option letter processing */

  	char	   *oli;			/* option letter list index */

! 	if (place[0] == '-')		/*Check if current argument is another option to be processed instead of path of file for -f */

! 	{

! 		place++;

! 		optopt = (int) *place++;

! 	}

! 	if (!*place && (optopt != 'f'))//introduced extra check so that if next argument received is 2nd file for -f it doesn't exit.

  	{							/* update scanning pointer */

  		if (optind >= argc)

  		{

*************** getopt_long(int argc, char *const argv[]
*** 157,162 ****
--- 161,168 ----
  		}

  	}

  

+ if ((optopt != 'f'))

+ {

  	/* short option */

  	optopt = (int) *place++;

  

*************** getopt_long(int argc, char *const argv[]
*** 198,202 ****
--- 204,217 ----
  		place = EMSG;

  		++optind;

  	}

+ }

+ else//to update the list of files to be read

+ {

+ 		optarg = argv[optind];

+ 		place = EMSG;

+ 		++optind;

+ 		place = argv[optind];

+ 

+ }

  	return optopt;

  }

#2Euler Taveira
euler@timbira.com
In reply to: Vikash3 S (#1)
Re: [patch] for "psql : Allow processing of multiple -f (file) options "

On 09-04-2012 02:43, Vikash3 S wrote:

Please find the patch regarding trivial changes against To Do item list for
"psql : Allow processing of multiple -f (file) options ".
Looking for valuable feedback.

Aren't you forget to cover the single transaction (-1) mode? How would you
handle ON_ERROR_* options? Look at the archives for references. Also, your
disclaimer doesn't seems attractive; make it clear you're contributing code
under the PostgreSQL license.

--
Euler Taveira de Oliveira - Timbira http://www.timbira.com.br/
PostgreSQL: Consultoria, Desenvolvimento, Suporte 24x7 e Treinamento