[freetds] port setting in the global section of the config file does not work

Christos Zoulas christos at zoulas.com
Thu Jun 17 15:44:42 EDT 2004


Hello,

Here's another patch that allows you:

1. to have sticky global flags [like the ones specified in go]
2. to have a quiet mode, where tsql does not spew and can be used in scripts

This patch adds:

   -o option that takes the same flags as go
   A -q flag to go, that prevents things informational messages printed
   to stdout.
   Eliminated the "changed language to" error message, which is really
   an informational message.

Note, that you can turn off/on the global settings on an individual go
command by specifying the option.

I.e. if headers were turned off globally via 'tsql -o h', then you
can turn headers back on for an individual command via 'go -h'

Enjoy,

christos

Index: tsql.c
===================================================================
RCS file: /src/twosigma/cvsroot/pub/devel/freetds/src/apps/tsql.c,v
retrieving revision 1.4
diff -u -u -r1.4 tsql.c
--- tsql.c	5 Mar 2004 20:10:34 -0000	1.4
+++ tsql.c	17 Jun 2004 19:38:52 -0000
@@ -74,10 +74,13 @@
 	OPT_VERSION = 0x01,
 	OPT_TIMER = 0x02,
 	OPT_NOFOOTER = 0x04,
-	OPT_NOHEADER = 0x08
+	OPT_NOHEADER = 0x08,
+	OPT_QUIET = 0x10
 };
 
 static int istty = 0;
+static int global_opt_flags = 0;
+#define QUIET (global_opt_flags & OPT_QUIET)
 
 int do_query(TDSSOCKET * tds, char *buf, int opt_flags);
 static void tsql_print_usage(const char *progname);
@@ -95,7 +98,8 @@
 	char line[1000];
 	int i = 0;
 
-	printf("%s", prompt);
+	if (!QUIET)
+	    printf("%s", prompt);
 	if (fgets(line, 1000, stdin) == NULL) {
 		return NULL;
 	}
@@ -232,19 +236,20 @@
 	int argc = 0;
 	int opt;
 
-	*opt_flags = 0;
 	/* make sure we have enough elements */
-	argv = (char **) malloc(sizeof(char *) * strlen(s));
+	argv = (char **) malloc(sizeof(char *) * (strlen(s) + 2));
 	if (!argv)
 		return 0;
 
 	/* parse the command line and assign to argv */
+	argv[argc++] = "tsql";
 	argv[argc++] = strtok(s, " ");
-	if (argv[0])
+	if (argv[argc - 1])
 		while ((argv[argc++] = strtok(NULL, " ")) != NULL);
 
+	*opt_flags = 0;
 	optind = 0;		/* reset getopt */
-	while ((opt = getopt(argc - 1, argv, "fhtv")) != -1) {
+	while ((opt = getopt(argc - 1, argv, "fhqtv")) != -1) {
 		switch (opt) {
 		case 'f':
 			*opt_flags |= OPT_NOFOOTER;
@@ -258,6 +263,9 @@
 		case 'v':
 			*opt_flags |= OPT_VERSION;
 			break;
+		case 'q':
+			*opt_flags |= OPT_QUIET;
+			break;
 		}
 	}
 	free(argv);
@@ -277,6 +285,7 @@
 	int opt;
 	const char *locale = NULL;
 	char *charset = NULL;
+	char *opt_flags_str = NULL;
 
 	setlocale(LC_ALL, "");
 	locale = setlocale(LC_ALL, NULL);
@@ -289,17 +298,12 @@
 		charset = nl_langinfo(CODESET);
 #endif
 
-	if (locale)
-		printf("locale is \"%s\"\n", locale);
-	if (charset) {
-		printf("locale charset is \"%s\"\n", charset);
-	} else {
-		charset = "ISO-8859-1";
-		printf("using default charset \"%s\"\n", charset);
-	}
 
-	while ((opt = getopt(argc, argv, "H:S:I:V::P:U:p:vC")) != -1) {
+	while ((opt = getopt(argc, argv, "H:S:I:V::P:U:p:vCo:")) != -1) {
 		switch (opt) {
+		case 'o':
+			opt_flags_str = optarg;
+			break;
 		case 'H':
 			hostname = (char *) malloc(strlen(optarg) + 1);
 			strcpy(hostname, optarg);
@@ -345,6 +349,26 @@
 		}
 	}
 
+	if (opt_flags_str != NULL) {
+		char *minus_flags = malloc(strlen(opt_flags_str) + 2);
+		if (minus_flags != NULL) {
+			*minus_flags = '-';
+			strcpy(&minus_flags[1], opt_flags_str);
+			get_opt_flags(minus_flags, &global_opt_flags);
+			free(minus_flags);
+		}
+	}
+
+
+	if (locale)
+		if (!QUIET) printf("locale is \"%s\"\n", locale);
+	if (charset) {
+		if (!QUIET) printf("locale charset is \"%s\"\n", charset);
+	} else {
+		charset = "ISO-8859-1";
+		if (!QUIET) printf("using default charset \"%s\"\n", charset);
+	}
+
 	/* validate parameters */
 	if (!servername && !hostname) {
 		fprintf(stderr, "Missing argument -S or -H\n");
@@ -418,7 +442,8 @@
 		return 0;
 	}
 
-	if (msg->msg_number != 5701 && msg->msg_number != 20018) {
+	if (msg->msg_number != 5701 && msg->msg_number != 5703
+	    && msg->msg_number != 20018) {
 		fprintf(stderr, "Msg %d, Level %d, State %d, Server %s, Line %d\n%s\n",
 			msg->msg_number, msg->msg_level, msg->msg_state, msg->server, msg->line_number, msg->message);
 	}
@@ -531,7 +556,8 @@
 		}
 		if (!strncasecmp(cmd, "go", 2)) {
 			line = 0;
-			get_opt_flags(s, &opt_flags);
+			get_opt_flags(s + 2, &opt_flags);
+			opt_flags ^= global_opt_flags;
 			do_query(tds, mybuf, opt_flags);
 			mybuf[0] = '\0';
 		} else if (!strcasecmp(cmd, "reset")) {



More information about the FreeTDS mailing list