[freetds] tsql bug fixes that got lost between 0.64 and 0.65

Christos Zoulas christos at zoulas.com
Thu Aug 24 11:14:42 EDT 2006


1. You cannot use readline when stdin is not a tty. You need to use the
   home-brewed version. If you don't believe me, try passing a script
   to tsql via stdin that has lines that begin with tab. Yes, you guessed
   it right, readline will helpfully give directory listings.
2. The check for optind == argc is in the wrong place so it always fires.
3. Use strcasecmp for all commands, not just go.

christos

Index: tsql.c
===================================================================
RCS file: /src/twosigma/cvsroot/external/public/freetds/src/apps/tsql.c,v
retrieving revision 1.1.1.7
retrieving revision 1.13
diff -u -r1.1.1.7 -r1.13
--- tsql.c	24 Apr 2006 21:10:42 -0000	1.1.1.7
+++ tsql.c	24 Aug 2006 15:10:19 -0000	1.13
@@ -80,6 +80,7 @@
 	OPT_QUIET =    0x10
 };
 
+static int istty = 0;
 static int global_opt_flags = 0;
 #define QUIET (global_opt_flags & OPT_QUIET)
 
@@ -91,11 +92,15 @@
 static void slurp_input_file(char *fname, char **mybuf, int *bufsz, int *line);
 
 #ifndef HAVE_READLINE
-static char *readline(char *prompt);
-static void add_history(const char *s);
+#define readline(a) my_readline(a)
+#define add_history(a) my_add_history(a)
+#endif
+
+static char *my_readline(char *prompt);
+static void my_add_history(const char *s);
 
 static char *
-readline(char *prompt)
+my_readline(char *prompt)
 {
 	char line[1024];
 	int i = 0;
@@ -116,10 +121,9 @@
 }
 
 static void
-add_history(const char *s)
+my_add_history(const char *s)
 {
 }
-#endif
 
 static int
 do_query(TDSSOCKET * tds, char *buf, int opt_flags)
@@ -195,7 +199,9 @@
 			}
 			break;
 		case TDS_STATUS_RESULT:
-			printf("(return status = %d)\n", tds->ret_status);
+			if (!QUIET)
+				printf("(return status = %d)\n",
+				    tds->ret_status);
 			break;
 		default:
 			break;
@@ -276,12 +282,10 @@
 			free(argv);
 			return 0;
 		}
-		
-		if (optind != argc) {
-			fprintf(stderr, "flags = %x [%d != %d]\n", *opt_flags, optind, argc);
-		}		
-
 	}
+	if (optind != argc) {
+		fprintf(stderr, "flags = %x [%d != %d]\n", *opt_flags, optind, argc);
+	}		
 	
 	free(argv);
 	return 1;
@@ -485,7 +489,10 @@
 		n = strrchr(s, '\n');
 		if (n != NULL)
 			*n = '\0';
-		add_history(s);
+		if (istty)
+		    	add_history(s);
+		else
+		    	my_add_history(s);
 		(*line)++;
 	}
 }
@@ -505,9 +512,12 @@
 	TDSCONNECTION *connection;
 	int opt_flags = 0;
 
+	istty = isatty(0);
+
 	/* grab a login structure */
 	login = tds_alloc_login();
 
+
 	context = tds_alloc_context(NULL);
 	if (context->locale && !context->locale->date_fmt) {
 		/* set default in case there's no locale file */
@@ -540,7 +550,10 @@
 		sprintf(prompt, "%d> ", ++line);
 		if (s)
 			free(s);
-		s = readline(QUIET ? NULL : prompt);
+		if (istty)
+			s = readline(QUIET ? NULL : prompt);
+		else
+			s = my_readline(QUIET ? NULL : prompt);
 		if (s == NULL) 
 			break;
 
@@ -575,24 +588,27 @@
 		if (!strcmp(cmd, "exit") || !strcmp(cmd, "quit") || !strcmp(cmd, "bye")) {
 			break;
 		}
-		if (!strcmp(cmd, "version")) {
+		if (!strcasecmp(cmd, "version")) {
 			tds_version(tds, mybuf);
 			printf("using TDS version %s\n", mybuf);
 			line = 0;
 			mybuf[0] = '\0';
 			continue;
 		}
-		if (!strcmp(cmd, "reset")) {
+		if (!strcasecmp(cmd, "reset")) {
 			line = 0;
 			mybuf[0] = '\0';
-		} else if (!strcmp(cmd, ":r")) {
+		} else if (!strcasecmp(cmd, ":r")) {
 			slurp_input_file(strtok(NULL, " \t"), &mybuf, &bufsz, &line);
 		} else {
 			while (strlen(mybuf) + strlen(s) + 2 > bufsz) {
 				bufsz *= 2;
 				mybuf = (char *) realloc(mybuf, bufsz);
 			}
-			add_history(s);
+			if (istty)
+			    	add_history(s);
+			else
+			    	my_add_history(s);
 			strcat(mybuf, s);
 			/* preserve line numbering for the parser */
 			strcat(mybuf, "\n");



More information about the FreeTDS mailing list